In the development process, often the same computer to develop different projects, different projects may require different versions of the package, in order to solve this problem leads to the virtual environment.
To configure a virtual environment:
1. Install the virtual environment:
sudo pip3 install virtualenv
2. Install the Virtual Environment Expansion pack:
sudo pip3 install Virtualenvwrapper
3. Modify the configuration file in the user home directory. BASHRC, add the following:
Export workon_home= $HOME//usr/local/bin/virtualenvwrapper.sh
4. Use the source. BASHRC command to make the configuration file effective.
You may receive an error like this:
/usr/bin/python:no module named Virtualenvwrapper Virtualenvwrapper.sh:There was a problem running the Initiali zation Hooks. If Python could not import the module Virtualenvwrapper.hook_loader, for virtualenvwrapper_ PYTHONis set
The system defaults to the PYTHON2 interpreter, which requires us to add the PYTHON3 interpreter to the environment variable:
4.1 Open the System environment variable file:
Vi/etc/profile
4.2 Adding Python3:
Virtualenvwrapper_python=/usr/bin/python3
4.3 Use the source/etc/profile command to make the configuration file effective.
4.4 Execute the source. BASHRC command again to make the configuration file effective.
5. The command to create the PYTHON3 virtual environment is as follows:
Mkvirtualenv-p Python3 Virtual Environment name
- Once created, it will automatically work in this virtual environment.
- Creating a virtual environment requires networking.
- Working on a virtual environment, the prompt "(Virtual environment Name)" appears at the front.
- All virtual environments are located in the hidden directory under/home/python/. Virtualenvs.
6. It is possible to complete the installation of the package required for the complete project that was previously used:
6.1 First pour the installation package from the previous project into a folder:
Pip Freeze > Package.txt
6.2 Switch to the newly created virtual environment and install the packages in Package.txt:
Pip Install-r./package.txt # puts Package.txt in the home directory, and the system switches to the home directory.
This situation may occur:
A red font appears, and then the installation stops. Workaround: Observe the red font description of which package installation has a problem, change the version number or directly commented out, and so on all installed and then install the package separately.
When the last few lines appear, it means that the installation was successful:
6.3 CD. Virtualenvs/django_py3/lib/python3.5/site-packages/can view all the packages that were just installed:
7. The command to exit the virtual environment is as follows:
Deactivate
8. The command to delete the virtual environment is as follows:
RMVIRTUALENV Virtual Environment Name
9. View the Virtual Environment command: Workon tab twice
10. Enter Virtual Environment command: Workon Virtual environment name
Configuration of the Python framework's virtual environment