Introduction to Python Virtualenv
Python has its own virtual environment, the virtual environment is the implementation of the implementation of the independent implementation of the environment, on the same server can create a different virtual environment for different systems, the project between the operating environment to maintain independence Not affected each other.For example, project A runs in a Python2-based environment, and project B can run in a Python-based environment. Python virtualenv tools manage virtual environments.
Install virtualenv
Instructions
After executing the command, it creates a folder in the current directory, which contains some Python implementation files, as well as a copy of the pip for installing the other packages.
In addition, when you create env can choose the specified Python interpreter, for example, the following is based on Python3 created a virtual environment.
By default, the virtual environment will depend on the system components in the system environment, that is, the system has been installed in a third party package will be installed in a virtual environment, if you do not want to rely on these packages, then you can add the parameters-no-site-packages to create a virtual environment
Activate the virtual environment
After the activation is successful, the command line will display the name of the virtual environment, similar to ((env)Your-Computer:your_project UserName$)
Exit the virtual environment
$ deactivate
If you want to delete the virtual environment, then you can directly run the rm -rf venv/ command.
Install Python packages in a virtual environment
Virtualenv comes with a pip installation tool, so the packages that need to be installed can run directly:
pip install [Package name]
If you do not start the virtual environment, the system environment also installed the pip tool, then the packages will be installed in the system environment, in order to avoid this, you can add in the ~ / .bashrc file:
export PIP_REQUIRE_VIRTUALENV=true
If you run pip without opening a virtual environment, you will be prompted with an error:
Could not find an activated virtualenv (required).
Virtualenvwrapper
Virtaulenvwrapper is an extension package for virtualenv for more convenient management of virtual environments. It can be done:
1. Integrate all virtual environments in a single directory
2. Manage (add, delete, copy) virtual environments
3. Switch virtual environments
Install Virtualenvwrapper
Before installing Virtualenvwrapper, virtualenv is required to be installed
$ pip install virtualenvwrapper
At this time can not use virtualenvwrapper, the default virtualenvwrapper installed in /usr/local/bin below, in fact, you need to run virtualenvwrapper.sh file.
Create a directory to store the virtual environment
export WORKON_HOME=$HOME/Envs source /usr/local/bin/virtualenvwrapper.sh 3. Run: $ source ~/.zshrc
At this point, virtualenvwrapper can be used. The basic use of virtualenvwrapper:
1. List the virtual environment list
workon or lsvirtualenv
Create new virtual environment mkvirtualenv [virtual environment name]
3. Remove the virtual environment
rmvirtualenv [virtual environment name]
deactivate