Why use virtual environment in python? (Virtual environment function and virtual environment construction, switching, exit, migration and packaging) code demonstration. Official venv usage (**)

Detailed graphic and text explanation of python’s virtual environment (virtual environment functions and virtual environment construction, switching, exit, migration and packaging) code demonstration https://blog.csdn.net/weixin_45440484/article/details/130144943

http://www.360doc.com/content/23/0828/21/1339386_1094251670.shtml

Why use a virtual environment?

First, a virtual environment helps keep your projects clean and separate. You don’t have to worry about one project’s dependencies breaking another, which makes project management easy.

Second, virtual environments make it easier to share projects. You can share your project’s virtual environment configuration to ensure that others can easily set up the same environment without worrying about dependencies.

illustrate:

? Python is a constantly evolving software with many versions;

py’s various application apps are all products built on different versions of python;

Although in theory the version should be compatible with lower versions, it is difficult to do so in python.

Sometimes, when different py applications need to run on the same computer, if a virtual environment is not used to isolate them, it is likely that “the dependency of one project will destroy another project.”

python venv: Officially introduced in version 3.3 or above.

python3 -m venv myvenv

virualenv: third-party library, can be used in python2/3.

conda: Part of the Anaconda distribution.

Python virtual environment (pipenv, venv, conda, all in one) [easy to understand]

https://cloud.tencent.com/developer/article/2124483

Summary:

3. Common tools

Knowing what a virtual environment is, you should know how to manage a virtual environment. This is inseparable from virtual environment management tools. Some commonly used tools are listed below, and the use of some tools will be briefly explained later. introduce.

  • Virtualenv virtualenv is a very popular Python virtual environment configuration tool. Not only does it support both python2 and python3, but you can specify a python interpreter for each virtual environment and choose not to inherit packages from the base version.
  • venv Taking into account the importance of the virtual environment, Python has a virtual environment module venv starting from version 3.3. For a detailed introduction to this module, please refer to PEP-405. Many of its operations are similar to virtualenv. If you are using a version before python3.3 or python2, you cannot use this function, and you need to use virtualenv for virtual environment management.
  • pipenv pipenv is the work of Kenneth Reitz (the author of requests). It combines Pipfile, pip, and virtualenv to effectively manage multiple Python environments and various packages. And windows are treated as first-class citizens.

4. The essence of virtual environment

The essence of the virtual environment is to modify the value of sys.path. Below are the values of sys.path in the virtual environment and those not in the virtual environment.

When the main difference lies in the path of the third-party package, one uses the site-packages of the virtual environment, while the other uses the site-packages of the python environment.

3. venv

Starting from version 3.3, Python comes with a virtual environment module venv. For a detailed introduction to this module, please refer to PEP-405 and .

1. Command overview

View venv help information:

python -m venv -h
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear] [--upgrade] [--without-pip] [--prompt PROMPT ] ENV_DIR [ENV_DIR ...]

Creates virtual Python environments in one or more target directories.

positional arguments:
  ENV_DIR A directory to create the environment in.

optional arguments:
  -h, --help show this help message and exit
  --system-site-packages
                        Give the virtual environment access to the system site-packages dir.
  --symlinks Try to use symlinks rather than copies, when symlinks are not the default for the platform.
  --copies Try to use copies rather than symlinks, even when symlinks are the default for the platform.
  --clear Delete the contents of the environment directory if it already exists, before environment creation.
  --upgrade Upgrade the environment directory to use this version of Python, assuming Python has been upgraded in-place.
  --without-pip Skips installing or upgrading pip in the virtual environment (pip is bootstrapped by default)
  --prompt PROMPT Provides an alternative prompt prefix for this environment.

Once an environment has been created, you may wish to activate it, e.g. by sourcing an activate script in its bin directory.

2. Create a virtual environment

python -m venv venv_demo

The two most used options here are:

  • ·--system-site-packages: Whether to include the site-packages of the Python system. After adding this option, the system’s site-packages will be added at the end of sys.path packages related directories. Without this option, system site-packages will not be included
  • --without-pip: Whether pip.exe is included in the virtual environment

3. Activation of virtual environment

There are two script texts activate.bat and deactivate.bat in the Scripts directory of the virtual environment, which are used to activate and exit the virtual environment respectively.

Note:

  • There are also these two script files in the virtual environment created by pipenv
  • There are multiple versions of the activate file. When using CMD on a Windows system, use activate.bat; if using PowerShell, use activate.psl.

When we activate the virtual environment, the Scripts directory of the virtual environment is added to the front of the system’s PATH path, which contains pip.exe and python.exe. The following conclusions can be drawn:

If you do not activate a virtual environment:

  • If you do not activate the virtual environment and directly run python.exe in the Scripts directory of the virtual environment, the interactive environment at this time will also be in this virtual environment.
  • If you do not activate the virtual environment and directly run pip.exe in the Scripts directory of the virtual environment to install the package, the installed package will also be installed in the virtual environment.

Note that PowerShell is used here, so when viewing the path environment variable, use $env:path. If it is CMD, you can use echo %PATH% to view the environment variable.

4. Management of virtual environment packages

There are two ways to use the pip tool to manage packages in a virtual environment.

  • The first way is to activate the virtual environment using activate in the virtual environment.
  • The second method does not activate the virtual environment, but the running Pip is designated as pip.exe in the Scripts directory in the virtual environment.

The basic use of the pip command will be introduced at the end of the article.

Summary of common pip commands

https://zhuanlan.zhihu.com/p/651163220

1. View version

pip --version # python2.x command
#############
pip3 --version # python3.x command

2. Upgrade pip version

When we install third-party libraries, we will sometimes be reminded to update pip. The following command can upgrade the pip command. The command is as follows:

pip install -U pip

3. View installed libraries

pip list
# Find a specific library
pip list | grep library name # mac system or Linux system
pip list | findstr library name # Windows system

Install third-party libraries

To install a third-party library, you need to run the pip install command. The following are several specific commands to install the library name:

 pip install [options] <requirement specifier> [package-index-options] ...
  pip install [options] -r <requirements file> [package-index-options] ...
  pip install [options] [-e] <vcs project url> ...
  pip install [options] [-e] <local project path> ...
  pip install [options] <archive url/path> ...

Direct installation

Directly run pip install [options] [package-index-options] to install the library you want. The example is as follows:

pip install selenium

Specified version installation

Sometimes we need to specify the version of the installation library, so we need to add version restrictions to the command

pip install package # Install the latest version by default
pip install package==3.141.0 #Specify version
pip install package>=3.141.0 # Minimum version

Specify source installation

By default, pip uses the official source address https://pypi.python.org/simple, but this source installation library may be slow, so we can specify domestic sources for installation to speed up the installation. Commonly used The domestic source address is as follows:

Tsinghua: https://pypi.tuna.tsinghua.edu.cn/simple

Alibaba Cloud: http://mirrors.aliyun.com/pypi/simple/

University of Science and Technology of China https://pypi.mirrors.ustc.edu.cn/simple/

Shandong University of Technology: http://pypi.sdutlinux.org/

Douban: http://pypi.douban.com/simple/

The installation command is as follows:

pip install -i https://pypi.douban.com/simple/ package

Install third-party libraries in batches through requirements files

After we pull the code from GitHub and other repositories, when we need to run the project locally, if the project has a requirements file, we only need to use this file to install the third parties needed for the project at once. Library.

pip install [options] -r [package-index-options]

Examples are as follows:

pip install -r requirements.txt

Download package but do not install

pip install <package name> -d <directory> pip install -d <directory> -r requirements.txt

Uninstall package

pip uninstall package pip uninstall -r requirements.txt

Update package

pip install --upgrade package pip install -U package # --upgrade can be abbreviated as -U

Show the directory where the package is located

pip show -f <package name>

Query upgradeable packages

pip list --outdated # List all expired libraries pip list -o # Abbreviation for --outdated, list all expired libraries

Uninstall pip

python -m pip uninstall pip

:https://zhuanlan.zhihu.com/p/651163220

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeHomepageOverview 388937 people are learning the system