User Tools

Site Tools


python:virtual_environments

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
python:virtual_environments [2014/11/25 14:14] kauffmanpython:virtual_environments [2016/06/30 11:56] (current) – [Other things you should know] kauffman
Line 4: Line 4:
 A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the “Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keeps your global site-packages directory clean and manageable. For example, you can work on a project which requires Django 1.3 while also maintaining a project which requires Django 1.0. ((http://docs.python-guide.org/en/latest/dev/virtualenvs)) A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the “Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keeps your global site-packages directory clean and manageable. For example, you can work on a project which requires Django 1.3 while also maintaining a project which requires Django 1.0. ((http://docs.python-guide.org/en/latest/dev/virtualenvs))
  
 +==== Other things you should know ====
 +  - https://github.com/pypa/virtualenv/pull/697
 +  - http://conda.pydata.org/docs/intro.html
 ==== Using Virtual Environments ==== ==== Using Virtual Environments ====
 The python virtual environment package is installed by default on most CS machines. To make sure you can do the following: The python virtual environment package is installed by default on most CS machines. To make sure you can do the following:
Line 33: Line 36:
  
 ==== Activate ==== ==== Activate ====
-Now you need to activate your virtual environment. This will setup some path variables to make the evironments bin and lib directory to be the default.+Now you need to activate your virtual environment. This will setup some path variables to make the environments bin and lib directory to be the default.
 <code> <code>
 user@computer:~/projects/exampleproject$ source bin/activate user@computer:~/projects/exampleproject$ source bin/activate
 (exampleproject)user@computer:~/projects/exampleproject$  (exampleproject)user@computer:~/projects/exampleproject$ 
-</code> 
-==== Deactivate ==== 
-To deactivate or stop working on your environment use the fuction that gets sourced when you activate your environment: 
-<code> 
-(exampleproject)user@computer:~/projects/exampleproject$ deactivate 
-user@computer:~/projects/exampleproject$  
 </code> </code>
  
-====== virtualenvwrapper ====== +==== Installing Modules ==== 
-virtualenvwrappers goal is to make virtualenv easier to use... sort of like when iTunes automatically organizes your iTunes library.+After activating the virtual environment you will notice that your prompt changed a little. This is not the only thing that has changed. Your $PATH and default python have changed as well (just checkout the bin/activate script to see what else it does).
  
-==== Basic Usage ==== +Notice that the 'bin' directory in your virtual environment has been prepended to $PATH and the python and pip executables are inside the bin directory.
-=== Create a virtual environment === +
-This creates the exampleproject folder inside ~/Envs.+
 <code> <code>
-user@computer:~/mkvirtualenv venv+(exampleproject)user@computer:~/projects/exampleprojectecho $PATH 
 +/home/user/projects/exampleproject/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
 </code> </code>
- 
-=== Work on a virtual environment === 
-'virtualenvwrapper' provides tab-completion on environment names. It really helps when you have a lot of environments and have trouble remembering their names. 
-'workon' also deactivates whatever environment you are currently in, so you can quickly switch between environments. 
 <code> <code>
-user@computer:~/workon venv+(exampleproject)user@computer:~/projects/exampleprojectwhich python 
 +/home/user/projects/exampleproject/bin/python 
 +</code> 
 +<code> 
 +(exampleproject)user@computer:~/projects/exampleproject$ which pip 
 +/home/user/projects/exampleproject/bin/pip
 </code> </code>
  
-Deactivating is still the same:+So to actually install your own python modules you can use 'pip' or 'easy_install' to do so:
 <code> <code>
-user@computer:~/deactivate+(exampleproject)user@computer:~/projects/exampleprojectpip install docopt 
 +Downloading/unpacking docopt 
 +  Downloading docopt-0.6.2.tar.gz 
 +  Running setup.py egg_info for package docopt 
 + 
 +Installing collected packages: docopt 
 +  Running setup.py install for docopt 
 + 
 +Successfully installed docopt 
 +Cleaning up...
 </code> </code>
-To delete:+ 
 +Notice where docopt was installed (~/projects/exampleproject/local/lib/python2.7/site-packages):
 <code> <code>
-user@computer:~/rmvirtualenv venv+(exampleproject)user@computer:~/projects/exampleprojectls -l local/lib/python2.7/site-packages/docopt* 
 +-rw------- 1 user group 19946 Mar  8 23:12 local/lib/python2.7/site-packages/docopt.py 
 +-rw------- 1 user group 26140 Mar  8 23:12 local/lib/python2.7/site-packages/docopt.pyc 
 + 
 +local/lib/python2.7/site-packages/docopt-0.6.2-py2.7.egg-info: 
 +total 40 
 +-rw------- 1 user group     1 Mar  8 23:12 dependency_links.txt 
 +-rw------- 1 user group    86 Mar  8 23:12 installed-files.txt 
 +-rw------- 1 user group 21525 Mar  8 23:12 PKG-INFO 
 +-rw------- 1 user group   651 Mar  8 23:12 SOURCES.txt 
 +-rw------- 1 user group     7 Mar  8 23:12 top_level.txt
 </code> </code>
  
-=== Other useful commands ===+ 
 +==== Deactivate ==== 
 +To deactivate or stop working on your environment use the function that gets sourced when you activate your environment:
 <code> <code>
-lsvirtualenv +(exampleproject)user@computer:~/projects/exampleproject$ deactivate 
-  List all of the environments. +user@computer:~/projects/exampleproject$ 
-cdvirtualenv +
-  Navigate into the directory of the currently activated virtual environment,  +
-  so you can browse its site-packages, for example. +
-cdsitepackages +
-  Like the above, but directly into site-packages directory. +
-lssitepackages +
-  Shows contents of site-packages directory.+
 </code> </code>
/var/lib/dokuwiki/data/pages/python/virtual_environments.txt · Last modified: 2016/06/30 11:56 by kauffman

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki