When developing Python projects, it is necessary to create multiple Python virtual environments in a virtual environment, because different projects require different resource bundles and related configurations.
Install the virtual environment
Steps:
Open the Linux terminal (shortcut key ctrl+alt+t) and enter the command:
sudo apt install python-virtualenv
sudo easy_install virtualenvwrapper
Note: The above two commands are executed one after the other, after completion of the virtual environment installation.
Or you can use Pip to install, as long as Pip is installed, and Python comes with a pip:
sudo pip install virtualenv
sudo pip install Virtualenvwrapper
Description: The result of the installation is identical, use any one.
Virtualenv is a virtual environment, Virtualenvwrapper Virtualenv is packaged to make it more friendly.
Note: The order of installation cannot be reversed, virtualenvwrapper must depend on virtualenv.
Create a virtual environment
Enter the following command at the terminal:
MKVIRTUALENV Virtual Environment Name
Such as:
Mkvirtualenv mywork
A virtual environment named MyWork is created
The created virtual environment is saved in the/home/.virtualenvs directory and can be viewed in this directory;
The virtual environment is automatically entered when the creation is successful, and the command line prompts to change;
To specify a python version to create a virtual environment
When the above command executes, the system will create the default Python version of the development environment, and generally everyone's computer will install the Python2 and Python3 versions at the same time, sometimes requiring different versions to create a virtual environment.
Mkvirtualenv-p python path Virtual environment name
Cases:
Mkvirtualenv-p/usr/bin/python3 MyWork
-P refers to the shorthand for path
The path of the py installation, which is installed by default in the/usr/bin/directory, and if it is not found at one time, use the command:
Whereis Python3
In the terminal command line search, the desired path appears.
Details: You do not need to go to the/home/.virtualenvs directory, you can execute commands under any directory, and the virtual environment is created in the same location.
Note: Creating a virtual environment is required to be done under networking, otherwise the creation fails.
View and use of virtual environments
Deleting a virtual environment
rmvirtualenv[Virtual Environment Name]
Cases:
Rmvirtualenvmywork
Note: If the current location is in a virtual environment, you need to exit the virtual environment before you can perform the delete
Note: You can perform a delete operation in any directory, and if you do not know the name, you can Rmvirtualen + two times tab to prompt all virtual environments
Exiting a virtual environment
Deactivate
Entering a virtual environment
workon[Virtual Environment Name]
Cases:
Workkonmywork
Note: Pressing workon+ two times the TAB key prompts all virtual environments.
View Python packages installed in a virtual environment
PIP List
Pip Freeze
Two commands can be queried, but the form of display is not the same.
Install Python packages in a virtual environment
Enter the virtual environment, in the terminal input:
The name of the PIP install package
Note: Be sure not to use sudo pip ..., this is to install the Python package in a virtual environment, if you use sudo permissions, the Python package will be installed in the host non-virtual environment, in the virtual environment can not find this package.
Installation and use of Python virtual environment under Linux system