Creation and migration of python virtual environment under ubuntu

## Creation of Python virtual environment
1. Download python3-venv
`` `python
sudo apt install python3-venv`
``
2. Create virtual environment
`` `python
python3 -m venv [virtual environment name]
` ``
3. Use Virtual environment
`` `python
# virtual environment name / bin directory
source activate`
``
4. Exit the virtual environment
`` `python
deactivate`
``

## Virtual environment migration
** Virtual environment copy and paste may cause problems , So generally do not use **, so you need to migrate the virtual environment. There are two known methods for migration, corresponding to online migration and offline migration.
#### 1. Online migration-there is a network
`` `python
# First activate the original virtual environment
source activate
# Extract the virtual environment dependencies to the file
pip freeze> requirements.txt
# In the target machine, create a new virtual environment
# in the new virtual environment reinstall the dependent modules
PIP install -r requirements.txt
`` `
#### 2. Offline migration-with or without network
`` `python
# First activate the original virtual environment
source activate
# Extract the virtual environment dependencies to the file
pip freeze> requirements.txt
# Download the offline installation package first in the original environment
pip download -d target folder-r requirements.txt
#Package the target folder to the target machine
# Create a new virtual environment in the target machine
# Activate and run the virtual environment on the target machine
# Bulk install the offline installation package
pip install --no -index --find-links = target folder-r requirements.txt
`` `

Guess you like

Origin www.cnblogs.com/coididy/p/12683152.html