Installation and use of Python virtual environment

You may encounter various library and module dependency issues when developing Python projects. To solve these problems, we can use virtual environments to isolate project dependencies. This time, we will introduce the installation and use of Python's native virtual environment and Anaconda virtual environment, and give an example of how to configure the virtual environment.

insert image description here

Python native virtual environment

Python's native virtual environment is implemented through the venv module. First of all, you need to ensure that the Python version is 3.3 and above. Next we will create and use a virtual environment with the following steps:

To create a virtual environment , use the following command line to create a virtual environment named my_project_venv in the current directory, and its python version is 3.8.

python -m venv my_project_venv python=3.8

For Windows users, activate the virtual environment

my_project_venv\Scripts\activate

For macOS and Linux users, activate the virtual environment and see the name of the virtual environment in front of the command prompt, indicating that the virtual environment has been activated.

source my_project_venv/bin/activate

The virtual environment installs dependent libraries, the same as normal installation

pip install numpy

Exit the virtual environment

deactivate

Anaconda virtual environment

Anaconda is a Python distribution for scientific computing that provides its own virtual environment management tool conda. Next, we will create and use the Anaconda virtual environment with the following steps:

To create a virtual environment , use the following command line to create a virtual environment named my_project_venv in the current directory, and its python version is 3.8.

conda create --name my_project_venv python=3.8

Activate the virtual environment and see the name of the virtual environment in front of the command prompt, indicating that the virtual environment has been activated.

conda activate my_project_venv

The virtual environment installs dependent libraries, the same as normal installation

pip install numpy

Exit the virtual environment

deactivate

Virtual environment variable configuration

Sometimes it is necessary to configure some environment variables or run some initialization scripts in the virtual environment. To this end, corresponding content can be added to the activation script of the virtual environment.

For Python native virtual environment:

  • For Windows users, my_project_venv\Scripts\activate.batthe file can be edited.
  • For macOS and Linux users, my_project_venv/bin/activatethe file can be edited.

For Anaconda virtual environment:

  • Whether you are a Windows or macOS and Linux user, you can edit my_project_venv/conda-meta/user-scripts.pyfiles.
  • In the corresponding file, you can add environment variables or run initialization scripts. For example, you can add the following to the activate script to configure an environment variable:
export MY_ENV_VAR=my_value

It is also possible to run some initialization scripts in the activation script. For example on a Linux or macOS system, the following can be added to run an my_init_script.shinit script called:

source my_init_script.sh

On Windows systems, you can use callthe command:

call my_init_script.bat

Through the above method, it is convenient to configure environment variables and initialization scripts for the virtual environment. This can be very useful in some projects that require specific settings.

Where to save the virtual environment

If you want to modify the storage location of the created python virtual environment, you can enter the directory of the C drive user to find a .condarcfile, open it with notepad++, and copy the following content.

auto_activate_base: false
channels:
  - defaults
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
show_channel_urls: True
ssl_verify: true
envs_dirs:
  - F:\PythonWorkProject\MyEnvsProject

Among them, the environment variable configuration description

  • envs_dirsSpecifies the directory where the virtual environment is created and stored.
  • ssl_verifyControls whether to verify SSL certificates for packages downloaded via the HTTPS protocol.
  • show_channel_urlsControls whether to display the channel URL when downloading packages.
  • auto_activate_baseSets whether to automatically activate the base environment when starting a new command prompt or terminal session.
  • channelsSpecifies the default package channel.
  • custom_channelsSpecify a custom package channel.
  • default_channelsSpecifies the default package channel.

Guess you like

Origin blog.csdn.net/qq_20288327/article/details/130665067