The most complete Python virtual environment usage

Posted by Alith7 on Wed, 24 Nov 2021 12:22:16 +0100

Write in front

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, 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

3. Activate the environment


$ cd venv
$ source ./bin/activate

Windows 10:

> cd venv
> .\Scripts\activate.bat

4. Exit the environment


$ 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

2, 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" >> ~/.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


5. Delete environment

rmvirtualenv venv

6. Other useful instructions

pip freeze #View the current installed library version
#Create the 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

3, 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
#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

4, 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, Pipfile files are automatically maintained, and Pipfile.lock is generated to lock the version and dependency information of the installation package to avoid construction errors. Compared with pip, it needs to manually maintain the installation package and version in requirements.txt, which is a 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.txt file manually.

3. Activate the Pipenv Shell

$ pipenv shell
$ python --version

Topics: Python Pycharm Optimize