Start from scratch and easily master the skills of pipenv to create a virtual environment
In Python development, virtual environment is a very useful concept. It can help us isolate the dependencies of different projects, avoid conflicts, and make the project more maintainable and portable. pipenv is a new Python package management tool that helps us create and manage virtual environments more easily. This article will introduce how to use pipenv to create a virtual environment from scratch and provide specific code examples.
Step 1: Install pipenv
First, we need to install pipenv. Execute the following command on the command line to install pipenv:
$ pip install pipenv
If you are using Python 3.4 or earlier, you may need to use the following command to install pipenv:
$ pip3 install pipenv
Step 2 : Create a virtual environment
Once pipenv is installed, we can create a new virtual environment. Go into the root directory of your project and execute the following command at the command line:
$ pipenv --python 3.7
This command will create a new virtual environment and specify it as Python 3.7 version.
Step 3: Install dependencies
After creating the virtual environment, we can start installing the project dependencies. Execute the following command on the command line to install dependencies:
$ pipenv install requests
This command will install the requests library as a dependency of the project. You can install other libraries according to actual needs.
Step 4: Enter the virtual environment
Once the dependencies are installed, we can enter the virtual environment to use these dependencies. Execute the following command:
$ pipenv shell
This command will activate the virtual environment and change your command line prompt to the name of the virtual environment.
Step 5: Use dependencies
Now we can use the dependencies we just installed to write code in the project. For example, create a file called main.py
and write the following code in it:
import requests response = requests.get('https://www.example.com') print(response.text)
Save and execute this file:
$ python main.py
If everything works fine, you You will see the content obtained from https://www.example.com.
Step 6: Exit the virtual environment
When you have completed the development of the project, you can exit the virtual environment by executing the following command:
$ exit
This will return you to your original environment and close the virtual environment.
Step 7: Clean up the virtual environment
If you want to clean up the virtual environment and remove all installed dependencies, you can execute the following command:
$ pipenv --rm
Note that this will be permanently deleted virtual environment and dependencies, so make sure you have backed up the necessary files before executing this command.
Summary
Using pipenv to create and manage virtual environments is a very convenient and efficient way. With a few simple steps, we can easily set up and use our own virtual environment. I hope that the specific code examples provided in this article can help you master the skills of using pipenv faster. I wish you greater success in Python development!
The above is the detailed content of Easily learn the steps to create a virtual environment using pipenv. For more information, please follow other related articles on the PHP Chinese website!