Go to Python virtualenv

Source: Internet
Author: User
Tags virtual environment virtualenv
Python virtualenv [10:14:13 | Author: yuyun] font size: large | medium | small virtualenv serves as sandbox, it implements multiple independent virtual environments by isolating the package directory and system environment parameters. This prevents too many third-party libraries from issues caused by version dependencies. At the same time, each independent virtual environment can be distributed by packaging, which greatly facilitates system deployment.
$ sudo easy_install virtualenv

Now we can create a virtual environment.

$ virtualenv test1New python executable in test1/bin/pythonInstalling setuptools............done.

We can see that the basic running environment has been installed in the virtual directory.

$ ls test1/binactivate  activate_this.py  easy_install  easy_install-2.6  pip  python$ ls test1/include/python2.6$ ls test1/libpython2.6$ ls test1/lib/python2.6/_abcoll.py   copy_reg.pyc     linecache.py     os.pyc         sre_compile.py     stat.py_abcoll.pyc  distutils        linecache.pyc    posixpath.py   sre_compile.pyc    stat.pycabc.py       encodings        locale.py        posixpath.pyc  sre_constants.py   types.pyabc.pyc      fnmatch.py       locale.pyc       re.py          sre_constants.pyc  types.pyccodecs.py    fnmatch.pyc      ntpath.py        re.pyc         sre_parse.py       UserDict.pycodecs.pyc   genericpath.py   ntpath.pyc       site-packages  sre_parse.pyc      UserDict.pycconfig       genericpath.pyc  orig-prefix.txt  site.py        sre.py             warnings.pycopy_reg.py  lib-dynload      os.py            site.pyc       sre.pyc            warnings.pyc

Go to the test1 directory and activate the virtual environment.

$ cd test1test1$ source bin/activate(test)test1$ which python/home/yuhen/projects/test1/bin/python(test)test1$ which easy_install/home/yuhen/projects/test1/bin/easy_install(test1)test1$ pythonPython 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)[GCC 4.4.3] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import sys>>> sys.path['', '/home/yuhen/projects/test1/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg', '/home/yuhen/projects/test1/lib/python2.6/site-packages/pip-0.7.2-py2.6.egg', '/home/yuhen/projects/test1/lib/python2.6', '/home/yuhen/projects/test1/lib/python2.6/plat-linux2', '/home/yuhen/projects/test1/lib/python2.6/lib-tk', '/home/yuhen/projects/test1/lib/python2.6/lib-old', '/home/yuhen/projects/test1/lib/python2.6/lib-dynload', '/usr/lib/python2.6', '/usr/lib64/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib64/python2.6/lib-tk', '/home/yuhen/projects/test1/lib/python2.6/site-packages', '/usr/local/lib/python2.6/dist-packages/virtualenv-1.4.9-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/simplejson-2.1.1-py2.6-linux-x86_64.egg', '/usr/local/lib/python2.6/site-packages', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages', '/usr/lib/pymodules/python2.6', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode']>>>

After "Souce bin/active" is activated, the command prompts that the line has an additional "(test1)" prefix. By default, Python and easy_install use programs in the bin directory of the virtual environment. SYS. Path: the Library directory of the current virtual environment is added to the search path list.

(test1)$ easy_install MySQL-pythonSearching for MySQL-pythonReading http://pypi.python.org/simple/MySQL-python/Reading http://sourceforge.net/projects/mysql-pythonBest match: MySQL-python 1.2.3c1Downloading http://sourceforge.net/.../1.2.3c1/MySQL-python-1.2.3c1.tar.gz/downloadProcessing downloadRunning MySQL-python-1.2.3c1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-Em0wfb/MySQL-python-1.2.3c1/egg-dist-tmp-vzoJ2tIn file included from _mysql.c:36:/usr/include/mysql/my_config.h:1050:1: warning: "HAVE_WCSCOLL" redefinedIn file included from /usr/include/python2.6/Python.h:8,                 from pymemcompat.h:10,                 from _mysql.c:29:/usr/include/python2.6/pyconfig.h:808:1: warning: this is the location of the previous definitionzip_safe flag not set; analyzing archive contents...Adding MySQL-python 1.2.3c1 to easy-install.pth fileInstalled /home/yuhen/projects/python/test1/lib/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-linux-x86_64.eggProcessing dependencies for MySQL-pythonFinished processing dependencies for MySQL-python(test1)$ ls -l lib/python2.6/site-packages/total 444-rw-r--r-- 1 yuhen yuhen    283 2010-06-02 09:46 easy-install.pth-rw-r--r-- 1 yuhen yuhen 106325 2010-06-02 09:46 MySQL_python-1.2.3c1-py2.6-linux-x86_64.eggdrwxr-xr-x 4 yuhen yuhen   4096 2010-06-02 09:45 pip-0.7.2-py2.6.egg-rw-r--r-- 1 yuhen yuhen 333447 2010-06-01 23:58 setuptools-0.6c11-py2.6.egg-rw-r--r-- 1 yuhen yuhen     30 2010-06-02 09:45 setuptools.pth(test1)$ cat lib/python2.6/site-packages/setuptools.pth./setuptools-0.6c11-py2.6.egg(test1)$ python>>> import MySQLdb>>> MySQLdb.version_info(1, 2, 3, 'gamma', 1)>>>

Mysql-python is installed in a virtual environment and the egg search path is correctly added to the easy-install.pth.

Finally, we can use the "deactivate" command to exit the virtual environment.

(test1)test1$ deactivate

When creating a virtual environment, we can add the "-- no-site-packages" parameter to indicate that the virtual environment does not access global site-packages.

$ virtualenv --no-site-packages test2New python executable in test2/bin/pythonInstalling setuptools............done.$ cd test2test2$ source bin/activate(test2)test2$ python>>> import sys>>> sys.path['', '/home/yuhen/projects/python/test2/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg','/home/yuhen/projects/python/test2/lib/python2.6/site-packages/pip-0.7.2-py2.6.egg', '/home/yuhen/projects/python/test2/lib/python2.6', '/home/yuhen/projects/python/test2/lib/python2.6/plat-linux2', '/home/yuhen/projects/python/test2/lib/python2.6/lib-tk', '/home/yuhen/projects/python/test2/lib/python2.6/lib-old', '/home/yuhen/projects/python/test2/lib/python2.6/lib-dynload', '/usr/lib/python2.6', '/usr/lib64/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib64/python2.6/lib-tk', '/home/yuhen/projects/python/test2/lib/python2.6/site-packages']>>>

In the search path, except for the python basic library path, Global Site-packages is no longer available. In this way, even if we have installed a large number of development kits, we can isolate a clean test environment.

To determine whether a virtual environment is -- no-site-packages, in addition to checking SYS. path, you can also determine through the "lib/python2.6/no-global-site-packages.txt" file.

Related Article

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.