- Version 18.04
Virtualenv Introduction
In the developmentWhen the application, system installation Python3 only one version: 3 .4 . All third-party packages will be pip installed Python3 the site-packages directory. If we want to develop multiple applications simultaneously, and that these applications will share a Python , it is installed in the system's Python 3. If the application A need Jinja 2 .7 , and the application B needs Jinja 2 .6 how to do? In this case, each application may need to have their own set of "independence" of the Python runtime environment. virtualenv is used to create a "quarantine" of an application for the Python runtime environment.
- Excerpt - Liao Xuefeng Python Tutorial: https://www.liaoxuefeng.com/wiki/1016959663602400/1019273143120480
Installation virtualenv
-
sudo pip3 install virtualenv
-
As the installation virtualenv need to use Python package management tool pip, and therefore need to be installed in advance pip, the command is as follows:
-
# Check whether the installation pip3 List # install command sudo install APT-GET-PIP python3 # view the version number pip3 installed in two ways: pip3 -V pip3 --version
Installation virtualenvwrapper
-
The main reason for the late virtualenvwrapper installation is easy, since every time you open a virtual environment need to find the directory where the virtual environment to run activate the use of source under the bin, such as:
-
source venv/bin/activate
-
This directory need to remember where each virtual environment, and switching environment is not easy, so we need to install virtualenvwrapper, this can be dispensed with each virtual operating environment of open switch directories, which is more convenient.
-
# Installation command sudo apt-get install virtualenvwrapper
Configuration virtualenvwrapper
-
Virtualenvwrapper After the installation is complete, you need to configure it to function properly
-
Files need to be configured is: /home/.bashrc
-
# Configure Vim .bashrc # add the following code file is opened in the bottom of the file Export VIRTUALENVWRAPPER_PYTHON = / usr / bin / to python3 Export WORKON_HOME the HOME = $ / .virtualenvs Source /usr/local/bin/virtualenvwrapper.sh
-
After configuration you need to perform the following command, before they can make the configuration files edited into force
-
source ~/.bashrc
Create a virtual environment
-
# Mkvirtualenv -p python3 virtual environment created in the name of mkvirtualenv -p python3 AlexLu_Django
-
Automatically enter the virtual environment created after just created, you can see the command line in front of the parentheses is the current name of the virtual environment
View virtual environment that has been created
-
lsvirtualenv
Entry / switched virtual environment
-
# Workon to enter / switch to the virtual environment name workon AlexLu_Django
Exit the current virtual environment
-
deactivate
Delete the virtual environment
-
# Rmvirtualenv need to delete the name of the virtual environment rmvirtualenv AlexLu_Flask
-
Note that not delete the current virtual environment, both need to exit To delete or switch to other virtual environments
Quickly into the virtual environment directory
-
cdvirtualenv
Quick installation directory into the virtual environment package
-
cdsitepackages
-
This directory is mainly used for managing virtual environments python package installed
sss