Linux command--virtualenv

Source: Internet
Author: User
Tags virtual environment virtualenv

Virtualenv can build a virtual and stand-alone Python environment, allowing each project environment to be isolated from other projects, keeping the environment clean, and resolving package conflict issues.

First, install the virtualenv

Virtualenv is actually a python package, so we can install it with Easy_install or PIP. The installation methods on the CentOS system are described below.

using the Easy_install installation method :

[[email protected] ~]# Yum install python-setuptools python-devel[[email protected] ~]# easy_install virtualenv

Use the PIP installation method:

[[email protected] ~]# easy_install pip[[email protected] ~]# pip install virtualenv

Use the Yum installation method:

[email protected] ~]# Yum install python-virtualenv
Ii. Creating a Python virtual environment

To create a Python virtual environment using the virtualenv command:virtualenv [Virtual environment name]

[Email protected] ~]# virtualenv env1new python executable in env1/bin/pythoninstalling setuptools, Pip...done.

Once executed, a folder with the same name as the virtual environment is generated locally.
If you have different versions of Python installed in your system, you can use the --python parameter to specify the Python version of the virtual environment:

[Email protected] ~]# virtualenv--python=/usr/local/python-2.7.8/bin/python2.7 env1running virtualenv with Interpreter/usr/local/python-2.7.8/bin/python2.7new python executable in env1/bin/python2.7also creating executable In Env1/bin/pythoninstalling Setuptools, Pip...done.

Measured by default, the virtual environment does not rely on the global site-packages of the system environment. For example, the system environment installed MYSQLDB module, in the virtual environment import MySQLdb will prompt importerror. If you want to rely on a third-party package for your system environment, you can use parameter--system-site-packages. Alternatively, you can use the Virtualenvwrapper toggleglobalsitepackages command to control whether the current environment uses global site-packages.

[Email protected] ~]# virtualenv--system-site-packages env1new python executable in env1/bin/pythoninstalling Setuptools, Pip...done.
Third, start the virtual environment

Enter the virtual environment directory and start the virtual environment as follows:

[Email protected] ~]# CD Env1/[[email protected] env1]# source Bin/activate (ENV1) [[email protected] env1]# Python-vpytho N 2.7.8

At this point the command line will be preceded by a parenthesis with the name of the virtual environment in parentheses. All modules installed later Easy_install or PIP will be installed in the virtual environment directory.

Iv. exiting the virtual environment

Exiting the virtual environment: deactivate

(ENV1) [Email protected] env1]# Deactivate[[email protected] env1]#
V. Use of Virtualenvwrapper

Virtualenvwrapper is a virtualenv extension tool that makes it easy to create, delete, copy, and switch between different virtual environments.

1. Installing Virtualenvwrapper
[Email protected] ~]# Easy_install virtualenvwrapper or: [[email protected] ~]# pip install Virtualenvwrapper

Note: This is not an operation in a virtual environment.

Create a folder to hold all the virtual environments:

[Email protected] ~]# mkdir ~/workspaces

Set the environment variable and add the following two lines to the ~/.BASHRC.

[Email protected] ~]# export Workon_home=~/workspaces[[email protected] ~]# source/usr/local/bin/ virtualenvwrapper.sh

Then you can use the Virtualenvwrapper. 2. Create a virtual Environment: mkvirtualenv [Virtual Environment name]

[Email protected] ~]# mkvirtualenv env1new python executable in env1/bin/pythoninstalling setuptools, Pip...done. (ENV1) [Email protected] ~]# mkvirtualenv env2new python executable in env2/bin/pythoninstalling setuptools, Pip...done. (ENV2) [Email protected] ~]#

Note: mkvirtualenv can use virtualenv parameters, such as--python, to specify the Python version. Once the virtual environment is created, it is automatically switched to this virtual environment. The virtual Environment directory is in Workon_home.

3. List Virtual environments: Lsvirtualenv-b
(ENV2) [Email protected] ~]# Lsvirtualenv-benv1env2
4. Start or switch Virtual environments: Workon [virtual Environment name]
(ENV2) [Email protected] ~]# Workon env1 (ENV1) [[email protected] ~]# echo $VIRTUAL _env/root/workspaces/env1
5. Check which packages are installed in the environment: lssitepackages
(ENV2) [Email protected] ~]# lssitepackageseasy_install.py  pip                  pkg_resources.py  setuptools_markerlib       Pip-1.5.4.dist-info  __pycache__       setuptools-2.2.dist-info
6. Enter the current environment directory: cdvirtualenv [subdirectory name]
(ENV1) [Email protected] ~]# cdvirtualenv (ENV1) [[email protected] env1]# pwd/root/workspaces/env1 (ENV1) [[email protected] env1]# Cdvirtualenv Bin (env1) [[email protected] bin]# Pwd/root/workspaces/env1/bin

Enter the Site-packages directory for the current environment: cdsitepackages [subdirectory name]

(ENV1) [Email protected] env1]# cdsitepackages (ENV1) [[email protected] site-packages]# pwd/root/workspaces/env1/lib/ Python2.6/site-packages (ENV1) [[email protected] site-packages]# cdsitepackages pip (env1) [[email protected] pip]# pwd/ Root/workspaces/env1/lib/python2.6/site-packages/pip
7. Control whether the environment uses global SITE-PACKAGES:TOGGLEGLOBALSITEPACKAGES8. Replicating a virtual Environment: cpvirtualenv [Source] [dest]
[Email protected] ~]# cpvirtualenv env1 env3copying env1 as Env3 ... (ENV3) [Email protected] ~]#
9. Exit the virtual environment: DEACTIVATE10. Delete Virtual Environment: rmvirtualenv [Virtual Environment name]
[Email protected] ~]# rmvirtualenv env2removing env2 ...
Vi. relevant points of knowledge

As you can see from this diagram, Windows-like EXE programs, in fact, python2.7, python3.4, and python3.4m are executable files. So as long as the/usr/bin is added to the environment variable path, in any directory can execute the three executable program, run a different version of Python.

For simplicity, a soft link is generated for each of the three executables. If you delete the current Python soft link and re-establish a soft link and point it to python3.4, running the python command at the terminal launches the python3.4 executable program.

Here's a question:

At the time of establishing the virtualenv, the Python version of the virtual environment is executed through--python, which can be seen in the directory of the virtual environment, but Python is found in the/usr/bin directory of the real system through the which Python command.

Linux command--virtualenv

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.