Create a virtual standalone Python environment under Ubuntu

  • 2020-05-26 09:31:14
  • OfStack

preface

Virtual environment is the independent execution environment when the program is executed. In the same server, different virtual environments can be created for different systems to use. The operation environment between projects remains independent and is not affected by each other. For example, projects can run in an Python2.7-based environment, while projects B can run in an Python3.x environment. Manage the virtual environment with the virtualenv tool in Python.

In addition, it is highly recommended to install virtual environment to manage your Python environment on win or mac. Virtual environment can bring you a lot of benefits. For example, on Mac, the Python environment is 2.7. The most suitable one for our Django development is 3.4+. This way, you have to go to Google how to uninstall or transfer to Python 3.4 environment, or more trouble. Once we have a virtual environment, we can install different versions of the modules or packages we need in a separate environment, which brings great convenience.

Install

Perform the following installation on the Linux system:


$ sudo pip install virtualenv

In Ubuntu and its derivative system, execute the following command to install:


$ sudo apt-get install python-virtualenv

Create

After the installation is successful, execute the following command to create a virtual environment named myvenv:


$ virtualenv myvenv

Here's a hint:


allen@ubuntu:~$ virtualenv myvenv
Running virtualenv with interpreter /usr/bin/python2
New python executable in myvenv/bin/python2
Also creating executable in myvenv/bin/python
Installing setuptools, pip...done.

Activate


source kvenv/bin/activate

The specific process is as follows. You can see that we are viewing the version of Python under the current environment, which shows that it is under 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 as follows:


deactivate

Pip

After activating the virtual environment, you can use any Pip in the environment:


pip install Pillow

Virtualenvwrapper

It is a virtual environment extension package for managing virtual environments, such as listing all virtual environments, deleting, and so on.

1. Installation:


# The installation virtualenv
(sudo) pip install virtualenv

# The installation virtualenvwrapper
(sudo) pip install virtualenvwrapper

2. The configuration:

Modify ~/.bash_profile or other environment variable related files (e.g..bashrc (my Ubuntu15.10 is this one) or.zshrc after ZSH) by adding the following statement:


export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/workspace
source /usr/local/bin/virtualenvwrapper.sh

Then run:


$ sudo apt-get install python-virtualenv
0

3. Usage:

mkvirtualenv zqxt: create a runtime zqxt workon zqxt: work in the zqxt environment or switch from another environment to the zqxt environment deactivate: exit the terminal environment

Other:

rmvirtualenv ENV: delete the running environment ENV mkproject mic: create mic project and run environment mic mktmpenv: create a temporary runtime environment lsvirtualenv: lists the available runtime environments lssitepackages: lists the packages installed in the current environment

The environment created is independent and does not interfere with each other, and pip can be used for package management without the permission of sudo.

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: