The entire process of creating a virtual independent Python environment in Ubuntu.
Preface
A virtual environment is an independent execution environment for program execution. Different virtual environments can be created on the same server for different systems. The Running Environments between projects are independent and independent of each other. For example, a project can run in a Python2.7-based environment, while project B can run in a Python3.x-based environment. Use virtualenv to manage Virtual Environments in Python.
In addition, we strongly recommend that you install a virtual environment on Windows or mac to manage your Python environment. The virtual environment can bring you many benefits. For example, on Mac, the built-in Python environment is 2.7. The most suitable for Django development is 3.4 +. In this way, you have to go to Google to uninstall or switch to the Python3.4 environment, which is still quite troublesome. Once we have a virtual environment, we can install different modules or packages in an independent environment, which will bring great convenience.
Install
Run the following command in Linux to install the SDK:
$ sudo pip install virtualenv
Run the following command in Ubuntu and its derivative systems to install it:
$ sudo apt-get install python-virtualenv
Create
After the installation is successful, run the following command to create a virtual environment named myvenv:
$ virtualenv myvenv
The prompt is as follows:
allen@ubuntu:~$ virtualenv myvenvRunning virtualenv with interpreter /usr/bin/python2New python executable in myvenv/bin/python2Also creating executable in myvenv/bin/pythonInstalling setuptools, pip...done.
Activate
source kvenv/bin/activate
The specific process is as follows. We can see that we can view the Python version in the current environment. It is displayed in the virtual environment myvenv:
allen@ubuntu:~$ source myvenv/bin/activate(myvenv)allen@ubuntu:~$ which python/home/allen/myvenv/bin/python
Of course, exit the current virtual environment with the following command:
deactivate
Pip
After activating the virtual environment, you can use any Pip in this environment:
pip install Pillow
Virtualenvwrapper
It is a virtual environment extension package used to manage virtual environments, such as list all virtual environments and delete them.
1. installation:
# Install virtualenv (sudo) pip install virtualenv # install virtualenvwrapper (sudo) pip install virtualenvwrapper
2. Configuration:
Modify ~ /. Bash_profile or other environment variable-related files (for example,. bashrc (under my Ubuntu15.10) or. zshrc after ZSH), add the following statement:
export WORKON_HOME=$HOME/.virtualenvsexport PROJECT_HOME=$HOME/workspacesource /usr/local/bin/virtualenvwrapper.sh
Then run:
source ~/.bash_profile
3. Usage:
- Mkvirtualenv zqxt: Create the runtime environment zqxt
- Workon zqxt: Work in the zqxt environment or switch from another environment to the zqxt Environment
- Deactivate: exit the terminal environment
Others:
- Rmvirtualenv ENV: Delete the running environment ENV
- Mkproject mic: Create a mic project and a running environment mic
- Mktmpenv: create a temporary running environment
- Lsvirtualenv: List available runtime Environments
- Lssitepackages: List packages installed in the current environment
The created environment is independent and does not interfere with each other. You can use pip to manage packages without the sudo permission.
Summary
The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.