Home > Backend Development > Python Tutorial > Creating a virtual environment: Getting started quickly with pipenv

Creating a virtual environment: Getting started quickly with pipenv

王林
Release: 2024-01-16 11:08:05
Original
766 people have browsed it

Creating a virtual environment: Getting started quickly with pipenv

Get started with pipenv quickly: Create your first virtual environment

In Python development, using virtual environments is a common practice, which can help us isolate Conflicts between projects and dependent packages allow projects to run independently. Pipenv is a Python package management tool that integrates the functions of virtual environment and dependent package management, which can simplify our development process and environment configuration. This article will introduce how to quickly get started with pipenv and create your first virtual environment.

1. Install pipenv

To use pipenv, you first need to install it. You can use pip to install and run the following command in the terminal:

$ pip install pipenv
Copy after login

After the installation is successful, you can verify whether pipenv is installed normally by running the following command:

$ pipenv --version
Copy after login

If pipenv can be displayed correctly Version information indicates successful installation.

2. Create a virtual environment

Now we start to create the first virtual environment. Enter your project directory on the command line and run the following command:

$ pipenv shell
Copy after login

After running the above command, pipenv will automatically create a new virtual environment and switch your terminal to that environment. You will find that the terminal prompt has changed, indicating that you have entered the virtual environment.

3. Install dependency packages

After we have a virtual environment, we can use pipenv to install and manage the project's dependency packages. Run the following command in the terminal to install a sample dependency package (such as requests):

$ pipenv install requests
Copy after login

After running the above command, pipenv will automatically download and install the dependency package, and generate a file named in the project directory. Pipfile file, which records the project’s dependency package information.

In addition to using the pipenv install command to install a single dependency package, you can also use the pipenv install -r requirements.txt command to install a requirements.txt Install dependent packages in batches from files.

4. Run the Python script

Now, we can run the Python script in the virtual environment. Create a file named main.py in the project directory with the following content:

import requests

response = requests.get('https://www.python.org')
print(response.status_code)
Copy after login

After saving and exiting the file, run the following command in the terminal to execute the script:

$ python main.py
Copy after login

The running result will output the status code of the target website.

5. Exit the virtual environment

When you have completed the development of the current project, you can exit the virtual environment. Run the following command in the terminal:

$ exit
Copy after login

After running the above command, you will exit the virtual environment and return to the original environment.

Summary:

Through the above steps, you have successfully created and used your first virtual environment. pipenv provides simple commands to manage project dependency packages, making our development process more efficient and convenient. I hope this article can help you get started with pipenv quickly and bring its value into play in future development.

The above is the detailed content of Creating a virtual environment: Getting started quickly with pipenv. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template