The entire process of creating a virtual independent Python environment in Ubuntu.

Source: Internet
Author: User

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:

  1. Mkvirtualenv zqxt: Create the runtime environment zqxt
  2. Workon zqxt: Work in the zqxt environment or switch from another environment to the zqxt Environment
  3. Deactivate: exit the terminal environment

Others:

  1. Rmvirtualenv ENV: Delete the running environment ENV
  2. Mkproject mic: Create a mic project and a running environment mic
  3. Mktmpenv: create a temporary running environment
  4. Lsvirtualenv: List available runtime Environments
  5. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.