Home > Article > Backend Development > 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.
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
After the installation is successful, you can verify whether pipenv is installed normally by running the following command:
$ pipenv --version
If pipenv can be displayed correctly Version information indicates successful installation.
Now we start to create the first virtual environment. Enter your project directory on the command line and run the following command:
$ pipenv shell
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.
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
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.
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)
After saving and exiting the file, run the following command in the terminal to execute the script:
$ python main.py
The running result will output the status code of the target website.
When you have completed the development of the current project, you can exit the virtual environment. Run the following command in the terminal:
$ exit
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!