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
Open Linux Terminal (shortcut key ctrl+alt+t), enter command
sudo apt install python-virtualenvsudo 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 virtualenvsudo pip install Virtualenvwrapper
- Description: The result of the installation is identical, use any one.
- Virtualenv is a virtual environment, Virtualenvwrapper virtualenv commands are encapsulated to make them more friendly
Note: The order of installation cannot be reversed, virtualenvwrapper must depend on virtualenv.
Create a virtual environment
- 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 example: 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
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.
Note: Using Mkvirtualenv to create a virtual environment, you may be prompted not to find the command, typically the script file is not joined to the system path, resolved as follows:
- Method One:
# find Virtualenvwrapper script file Whereis virtualenvwrapper# Enter the directory CD/ usr/local/python3/bin# Execute script file activation Virtualenvwrappersource virtualenvwrapper.sh# can use commands correctly
- method Two:
# method requires manual activation after each reboot, inconvenient, add command to environment variable sudo vi ~/.bashrc# add a line at the end source/usr/local/python3/bin/virtualenvwrapper.sh# After saving, executing the source ~/.bashrc# can now use the command, and each time you restart the host auto-load command
Virtual environment View and use
- Delete virtual environment
rmvirtualenv [Virtual Environment Name] Example: Rmvirtualenv mywork
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] Example: Workkon mywork
Note: Pressing workon+ two times the TAB key prompts all virtual environments.
- View Python packages installed in a virtual environment
Pip Listpip Freeze
Two commands can be queried, but the form of display is not the same
Install Python packages in a virtual environment
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 Ubuntu system