Home >Backend Development >Python Tutorial >Quickly learn how pipenv creates a virtual environment!

Quickly learn how pipenv creates a virtual environment!

WBOY
WBOYOriginal
2024-01-04 12:05:56900browse

Quickly learn how pipenv creates a virtual environment!

Let’s learn how pipenv creates a virtual environment efficiently!

During the development process, we often need to use different Python packages and dependencies in different projects. To avoid conflicts with these packages and dependencies, we can use virtual environments. The virtual environment can manage our packages and dependencies independently of the global environment of the system, ensuring the independence of each project.

pipenv is a Python virtual environment and package management tool. Through pipenv, you can easily create and manage virtual environments, and automatically handle the installation and version control of dependencies. Next, I will introduce how to use pipenv to efficiently create a virtual environment.

First, we need to install pipenv. Open a command line terminal and run the following command to install pipenv:

pip install pipenv

After the installation is complete, we can create a new virtual environment. Go to the directory where you want to create the virtual environment and run the following command:

pipenv shell

This will create a new virtual environment and activate it. You can see the name of the virtual environment in front of the command line prompt, indicating that you are using the virtual environment.

Next, install the dependency packages in the virtual environment. Suppose you want to install a package called requests, just use the following command:

pipenv install requests

This will automatically install the latest version of the requests package and add it Added to the virtual environment's dependency list.

If you need to install a specific version of the package, you can use the == operator to specify the version number. For example, to install version 2.27.1 of requests, you can run the following command:

pipenv install requests==2.27.1

You can also use , <code>> , , <code>>= and other operators to specify the version range to meet different needs.

If you are installing dependencies from an existing requirements.txt file, you can use the following command:

pipenv install -r requirements.txt

This will automatically install dependencies from requirements.txt Read the dependency list in and install the corresponding package.

During the development process, we may continue to add and delete dependent packages. In order to ensure the consistency between the current environment and the dependency list, we can use the following command to generate the Pipfile.lock file:

pipenv lock

Pipfile.lockThe file will contain the current Detailed information about the environment, including the version number and dependencies of each dependent package. When restoring the environment later or sharing the project with other developers, simply restore the environment via the Pipfile.lock file.

When we no longer need a dependent package, we can use the following command to uninstall it:

pipenv uninstall package_name

This will remove the package from the dependency list and remove it from the virtual environment uninstall.

In addition to the commands introduced above, pipenv also provides many other useful functions. For example, you can use the following command to view the installed packages and versions in the current virtual environment:

pipenv graph

You can also use the following command to run a command, such as executing a Python script:

pipenv run python script.py

This will run the specified command in the virtual environment.

To sum up, pipenv is a powerful Python virtual environment and package management tool. Through pipenv, we can easily create and manage virtual environments and automatically handle dependency installation and version control. I hope this article can help you use pipenv to create virtual environments more efficiently and improve your development efficiency!

The above is the detailed content of Quickly learn how pipenv creates a virtual environment!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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