python virtual environment

For Python development projects, using a virtual environment is the recommended best practice. By creating a virtual environment, you can isolate project tools and avoid version conflicts with other project tools. For example, you may be maintaining an old Web project that requires Django 1.2 Web framework, but then you will work on a new project using Django 2.2. If you update Django globally outside the virtual environment, you may encounter some version control problems in the future. In addition to preventing unexpected version conflicts, virtual environments allow packages to be installed and managed without administrative privileges.

Installing virtual environments using pip

  1. Open the Ubuntu terminal and enter Python 3 -- version to confirm that Python 3 is installed. This should return the python version number. If you need to update the python version, first update the Ubuntu version by entering sudo apt update & & sudo apt upgrade, and then update python with sudo apt upgrade python3.

  2. Install PIP by entering: python3 -m pip install --user --upgrade pip. After that, we can check the PIP version through Python 3 - M PIP -- version. You can keep PIP up to date with the following command: py -m pip install --upgrade pip

  3. Install venv by entering: Python 3 - M PIP install -- user virtualenv.

Create virtual environment

venv (Python 3) and virtualenv (python2) mainly describes how python3 creates a virtual environment.

Open the terminal, enter the project folder, and use the following command to create a virtual environment:

# On macOS and Linux:
python3 -m venv env
# On Windows:
py -m venv env
'''
Second parameter env Is the location where virtual parameters are created, usually using env.  venv Will be env Install in folder python Virtual environment.
Note: usually we should.gitignore Exclude from file git Dependency, ignore env catalogue
'''

Activate virtual environment

You must activate the virtual environment before installing and using dependent packages. Activating the virtual environment will add the execution environment of python and pip to the PATH of the shell

# On macOS and Linux:
source env/bin/activate
# On Windows:
.\env\Scripts\activate

Once activated, you can check the python parser to determine the location of the env

# On macOS and Linux:
which python
# .../env/bin/python
​
# On Windows:
where python
# .../env/bin/python.exe

Shut down the virtual environment

To shut down a virtual environment, simply run a simple command:

deactivate

 

Install dependent packages

When the virtual environment is installed and activated, we can install the dependencies required by the project through the following command:

python3 -m pip install you_lib

Install the specified version:

# Specify to install the 2.18.4 release version of request lib
python3 -m pip install requests==2.18.4
# Install 2.0 of the latest requests lib Version of X release
python3 -m pip install requests>=2.0.0,<3.0.0
# Install pre release version and add -- pre (pre release) parameter 
python3 -m pip install --pre requests
# Install dependencies from source code
cd google-auth
python3 -m pip install .
# Install from local archives
python3 -m pip install requests-2.18.4.tar.gz
# Specify locally to find the installation from the relevant directory
python3 -m pip install --no-index --find-links=/local/dir/ requests

Update the specified lib:

python3 -m pip install --upgrade requests

Installing dependencies using files

pip can declare all dependencies in a file without separate installation. For example, we can create a requirements Txt file contains:

requests==2.18.4
google-auth==1.1.0

Use the - r flag to install all dependencies

python3 -m pip install -r requirements.txt

View dependency list

The free command can list the names and version numbers of all installed lists

python3 -m pip freeze

 

Keywords: Python pip

Added by ironman32 on Tue, 01 Feb 2022 19:48:20 +0200