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
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
- Enter the following command at the terminal: mkvirtualenv Virtual Environment name
For example: mkvirtualenv mywork //Creates a virtual environment named MyWork
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: mkvirtualenv-p python path virtual environment name
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.
Example: mkvirtualenv-p/usr/bin/python3 mywork
-P refers to the path of the abbreviated PY installation, which is installed by default in the/usr/bin/directory;
If you can't find one at a 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.
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 this directory Cd/usr/local/python3/bin
# Execute script file activation virtualenvwrapper source virtualenvwrapper.sh
# You can use commands normally.
Method Two:
# method One after each reboot requires manual activation, inconvenient, add command to environment variable sudo vi ~/.bashrc
# Add a line at the end source/usr/local/python3/bin/virtualenvwrapper.sh
# Save after executing source ~/.BASHRC
# Now you can use commands normally, and each time you restart the host auto-load command
View and use of virtual environments
- Deleting a 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 the virtual environment deactivate
- Enter 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
Enter virtual environment, enter in Terminal: Pip install package name
Note: Be sure not to use sudo pip ..., here is the Python package installed in the virtual environment;
If sudo permission is used, the Python package is installed in a host non-virtual environment and cannot be found in the virtual environment
Installation and use of Python virtual environment under Ubuntu system