Create virtual standalone Python environment under Ubuntu whole process

Source: Internet
Author: User
Tags virtual environment virtualenv
Objective

A virtual environment is a stand-alone execution environment for program execution, where different virtual environments can be created on the same server for different systems to use, and the operating environment between projects remains independent and unaffected by one another. For example, a project can run in an Python2.7-based environment, and project B can run in an python3.x-based environment. Manage your virtual environment in Python with the Virtualenv tool.

In addition, it is highly recommended to install a virtual environment to manage your Python environment on win or Mac, the virtual environment can bring you a lot of benefits, such as on Mac, the self-brought Python environment is 2.7. And the most appropriate thing for our Django development is 3.4+. In this way, you will have to go to Google how to uninstall or go to the Python3.4 environment, or more trouble. Once we have a virtual environment, we can install the modules we need or the different versions of the packages in a separate environment, which will bring great convenience.

Install

Install the following command on the Linux system:

$ sudo pip install virtualenv

Install the following commands in Ubuntu and in their derived systems:

$ sudo apt-get install python-virtualenv

Create

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

$ virtualenv myvenv

Tips are 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 process is as follows, we can see the version of Python in the current environment, shown 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 have any pip in this 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:

#安装virtualenv (sudo) pip install virtualenv #安装virtualenvwrapper (sudo) pip install Virtualenvwrapper

2. Configuration:

Modify ~/.bash_profile or other environment variable related files (such as. BASHRC (This is under my Ubuntu15.10) or use ZSH. ZSHRC) and 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: Creating a Running environment ZQXT

Workon ZQXT: Working in a ZQXT environment or switching from another environment to a ZQXT environment

Deactivate: Exiting the terminal environment

Other:

Rmvirtualenv ENV: Remove the operating environment env

Mkproject Mic: Create MIC Project and run environment mic

Mktmpenv: Creating a temporary runtime environment

LSVIRTUALENV: List the available run environments

Lssitepackages: Lists the packages that are installed in the current environment

The created environment is independent, non-intrusive, and requires no sudo permission to manage the package using PIP.

Summarize

The above is the entire content of this article, I hope that the content of this article on everyone's study or work can bring certain help, if there is doubt you can message exchange.

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.