python virtual environment management summary

Creation and use of python virtual environment

As a newcomer, I never thought about Python virtual environment management. I thought that all the libraries I want to use are installed together. How good it is to use direct import. However, later, I learned more and more. I not only shed tears of regret, but this time, I caught all the methods of Python virtual environment management. Please choose which method you like. Again, virtual environments are important.

1, Use python commands directly

python -m venv [virtual environment name]

Enter the directory to create the virtual environment, execute the above command, and the corresponding virtual environment will be automatically created in the current directory. The activation of the virtual environment is the same as the following methods

2, Using virtualenv

1. Use pip

pip install virtualenv

2. Create a running environment

virtualenv [Virtual environment name] 
virtualenv venv

#If you do not want to use the system's packages, add the – no site packages parameter
virtualenv  --no-site-packages Create pathname

# Specify the python version. You need to install the corresponding Python version in advance
virtualenv test --python=python3.7

3. Activate the environment (the next time you want to enter the virtual environment, you also need to go through this step)

linux:

$ cd venv
$ source ./bin/activate

Windows 10:

> cd venv
> .\Scripts\activate.bat

4. Exit the environment

linux:

$ deactivate

Windows 10:

> .\Scripts\deactivate.bat

5. Delete environment

Before using the virtual envwrapper, you can directly delete the venv folder to delete the environment

6. Service environment

After entering the environment, all operations are the same as using python normally. The installation package uses pip install package

3, Using Virtualenvwrapper

Virtaulenvwrapper is an extension package of virtualenv. It is used to make it easier to manage virtual environments. It can do: - consolidate all virtual environments in one directory - manage (add, delete, copy) virtual environments - quickly switch virtual environments

1. Installation

# on Windows
pip install virtualenvwrapper-win
# on macOS / Linux
pip install --user virtualenvwrapper
# then make Bash load virtualenvwrapper automatically
echo "source virtualenvwrapper.sh" >> ~/.bashrc
source ~/.bashrc

2. Create a virtual environment

# on macOS/Linux:
mkvirtualenv --python=python3.6 venv
# on Windows
mkvirtualenv --python=python3 venv

3. Activate the environment

workon #List virtual environments
workon [venv] #Switch environment

4. Exit the environment

deactivate

5. Delete environment

rmvirtualenv venv

6. Other useful instructions

pip freeze #View the current installed library version
#Create requirements Txt file, which contains a simple list of all packages and their versions in the current environment
#Keep the deployment the same and install all packages with one click
pip install -r requirements.txt
pip freeze > requirements.txt 
lsvirtualenv    #List all environments
cdvirtualenv    #Navigate to the directory of the currently active virtual environment, which is equivalent to the pushd directory
cdsitepackages   # Similar to the above, go directly to the site packages directory
lssitepackages     #Displays the contents of the site packages directory

4, Using conda management

conda can directly create virtual environments of different python versions. virtualenv mentioned earlier only specifies to create virtual environments with different versions of python. The premise is that different versions of python have been installed on your computer, which is not as flexible as conda.

1. Installation

Download python installed by anaconda, and you can use the conda tool directly

2. Create a virtual environment

Create different python versions and write the version number directly. You can also install the desired libraries at the same time.

# Python 2.7  
$ conda create -n venv python=2.7  

# Python 3.4  
$ conda create -n venv python=3.4  

# Python 3.5  
$ conda create -n venv python=3.5

3. Activate virtual environment

#on windows
activate venv
#on linux
source activate venv

4. Exit the virtual environment

#on windows
deactivate
#on linux
source deactivate

5. Delete virtual environment

# Delete an existing environment
conda remove --name venv --all

6. Other useful instructions

# Lists the virtual environments that exist in the system
conda info -e
conda env list

# View installed packages in the current environment
conda list

# View installed packages for a specified environment
conda list -n venv

# Find package information
conda search numpy

# Install package
conda install -n venv numpy
# If you do not -n specify an environment name, it is installed in the currently active environment
# You can also specify to install through a channel through - c

# Update package
conda update -n venv numpy

# Delete package
conda remove -n venv numpy

V Manage with pipenv

Pipenv is the package management tool officially recommended by Python. It integrates the functions of virtualenv, pip and pyenv. Ability to automatically create and manage virtual environments for projects. If you have used the requests library, you will love this library because it is produced by the same God. Pipenv uses Pipfile and Pipfile Lock to manage dependent packages. When using pipenv to add or delete packages, automatically maintain Pipfile files and generate Pipfile Lock to lock the version and dependency information of the installation package to avoid build errors. Compared with pip, you need to maintain requirements manually Txt has made great progress.

1. Installation

pip install pipenv

2. Create a virtual environment

$ cd myproject
$ pipenv install # Create environment
$ pipenv install requests # Or install the library directly

If there is no pipfile, a pipfile will be generated, and if some libraries are added, the file will be edited automatically. We will not update the requirements manually Txt file.

3. Activate the Pipenv Shell

$ pipenv shell
$ python --version
 
 
 

Keywords: Python

Added by matrixd on Tue, 11 Jan 2022 03:59:09 +0200