Python Virtual Environment virtualenv use tutorial

Source: Internet
Author: User
Tags virtual environment virtualenv
Virtualenv is used to create a stand-alone Python environment where multiple python is independent from each other and can:
1. Install the new kit without permission
2. Different applications can use different versions of the kit
3. Package upgrade does not affect other applications

Installation
Copy the Code code as follows:


sudo apt-get install python-virtualenv

How to use
[Code]
virtualenv [Virtual Environment name]


For example, create a **env** virtual environment
Copy CodeThe code is as follows:


Virtualenv ENV


By default, the virtual environment relies on site packages in the system environment, which means that the third-party package that is already installed in the system is installed in the virtual environment, and if you do not want to rely on the package, you can add parameters- No-site-packages setting up a virtual environment
Copy CodeThe code is as follows:


virtualenv--no-site-packages [Virtual environment name]


Start the virtual environment
Copy CodeThe code is as follows:


CD ENV
source./bin/activate


Note that at this point the command line one more (env), Env is the virtual environment name, then all the modules will only be installed in the directory.

Exiting a virtual environment
Copy the Code code as follows:


Deactivate


Installing a Python suite in a virtual environment
The virtualenv comes with the PIP installation tool, so the kit that needs to be installed can be run directly:
Copy CodeThe code is as follows:


PIP install [package name]


If the virtual environment is not started and the PIP tool is installed, the package will be installed in the system environment, in order to avoid this, you can add the following in the ~/.BASHRC file:
Copy CodeThe code is as follows:


Export Pip_require_virtualenv=true


or let the system automatically turn on the virtual environment when the PIP is executed:
Copy CodeThe code is as follows:


Export Pip_respect_virtualenv=true
Virtualenvwrapper


Virtaulenvwrapper is an expansion pack for virtualenv that makes it easier to manage virtual environments and can do:
1. Integrate all virtual environments into one directory
2. Manage (add, delete, copy) virtual environments
3. Switch the virtual environment
4 .....

Installation
Copy the Code code as follows:


sudo easy_install virtualenvwrapper


At this time can not use Virtualenvwrapper, the default virtualenvwrapper installed under/usr/local/bin, in fact, you need to run the virtualenvwrapper.sh file before the line, do not worry, open this file to see, There are installation steps, we set the environment according to the operation.

Create a directory to hold a virtual environment
Copy the Code code as follows:


mkdir $HOME/.virtualenvs


Add rows in ~/.BASHRC: Export workon_home= $HOME/.virtualenvs

Add rows in ~/.BASHRC: source/usr/local/bin/virtualenvwrapper.sh

Run: Source ~/.BASHRC

At this point the Virtualenvwrapper is ready to use.

List Virtual Environments
Copy the Code code as follows:


Workon


You can also use
Copy CodeThe code is as follows:


Lsvirtualenv


Create a new virtual environment
Copy CodeThe code is as follows:


mkvirtualenv [Virtual Environment name]


Start/Switch Virtual environments
Copy CodeThe code is as follows:


Workon [Virtual Environment name]

Deleting a virtual environment
Copy the Code code as follows:


rmvirtualenv [Virtual Environment name]


Leave the virtual environment
Copy CodeThe code is as follows:


Deactivate

--------------------------------------------------------------------------------------------------------------- ---------------------------------------------

Virtualenv to resolve dependencies, versions, and indirect permissions by creating tools for a standalone python development environment
Problem. For example, if a project relies on Django1.3 and the current global development environment is Django1.7, the version span is too large, causing incompatibilities to prevent the project from running, and using virtualenv to solve these problems.

Virtualenv creates an environment with its own installation directory, which does not share libraries with other virtual environments, makes it easy to manage Python versions and manage Python libraries

1. Installing virtualenv

Use Pip to install virtualenv, used Python should know the PIP package management artifact, even if do not know, the site has a lot of tutorials, but recommended to view the official installation Guide

Copy the Code code as follows:


$ pip Install virtualenv
Or, because of permissions issues, use sudo to temporarily elevate permissions
$ sudo pip install virtualenv

2. VIRTUALENV Basic Use
Start using virtualenv to manage the Python environment now
Copy the Code code as follows:


➜test git: (master) ✗virtualenv ENV #创建一个名为ENV的目录, and installed Env/bin/python, created Lib,include,bin directory, installed PIP
New python executable in
Installing Setuptools, Pip...done.
➜test git: (master) ✗CD ENV
➜env git: (master) ✗ll
Drwxr-xr-x Andrew_liu Staff 476 8 08:49 Bin
Drwxr-xr-x 3 Andrew_liu Staff 102 8 08:49 include
Drwxr-xr-x 3 Andrew_liu Staff 102 8 08:49 Lib

LIB, all installed Python libraries will be placed under lib/pythonx.x/site-packages/in this directory.
Bin,bin/python is the Python interpreter that is used in the current environment

If you run Virtualenv--system-site-packages ENV on the command line, all the libraries under/usr/lib/python2.7/site-packages are inherited, Latest version virtualenv take access to global site-packages as the default behavior
Default behavior.

2.1. Activating virtualenv
Copy the Code code as follows:


#ENV目录下使用如下命令
➜env git: (Master) ✗source./bin/activate #激活当前virtualenv
(ENV) ➜env git: (master) ✗ #注意终端发生了变化

Copy the Code code as follows:


#使用pip查看当前库
(ENV) ➜env git: (master) ✗pip list
Pip (1.5.6)
Setuptools (3.6)
Wsgiref (0.1.2) #发现在只有这三个
Pip Freeze #显示所有依赖
Pip Freeze > Requirement.txt #生成requirement. txt files
Pip install-r requirement.txt #根据requirement. txt to generate the same environment

2.2. Close Virtualenv

Use the following command
Copy the Code code as follows:


$ deactivate


2.3. Specify the Python version

You can use the-P python_exe option to specify a PYTHON version when creating a virtual environment


Copy the Code code as follows:


#创建python2.7 Virtual Environments
➜test git: (master) ✗virtualenv-p/usr/bin/python2.7 ENV2.7
Running virtualenv with interpreter/usr/bin/python2.7
New python executable in Env2.7/bin/python
Installing Setuptools, Pip...done.


Copy the Code code as follows:


#创建python3.4 Virtual Environments
➜test git: (master) ✗virtualenv-p/usr/local/bin/python3.4 ENV3.4
Running virtualenv with interpreter/usr/local/bin/python3.4
Using base prefix '/library/frameworks/python.framework/versions/3.4 '
New python executable in env3.4/bin/python3.4
Also creating executable in Env3.4/bin/python
Installing Setuptools, Pip...done.

This can already resolve Python version conflict issues and different versions of the Python library.

3. Other

3.1. Generate a packaged environment

Some special needs, may not have the network, we expect to package an env directly, can be extracted directly after use, this time can use the virtualenv-relocatable command to change env to a modifiable location env

Copy the Code code as follows:


#对当前已经创建的虚拟环境更改为可迁移
➜env3.4 git: (master) ✗virtualenv--relocatable.
Making script./bin/easy_install relative
Making script./bin/easy_install-3.4 relative
Making script./bin/pip relative
Making script./bin/pip3 relative
Making script./bin/pip3.4 relative

3.2. Get help
Copy the Code code as follows:


$ virtualenv-h

The current env is modified to a relative path, you can package the current directory, upload to another location to use
This does not enable the virtual environment to be used across platforms

4. Reference links
VIRTUALENV Official Document Http://virtualenv.readthedocs.org/en/latest/virtualenv.html

  • 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.