Sunday, November 10, 2013

Python VirtualEnv for Safe Playground

Python is great for experimentation. But sooner or later if you experiment too much with cutting edge libraries you face the challenge of maintaining a stable environment for your regular work. Python Virtualenv is a great way to encapsulate your package into a virtual environment, do what you need to do, then remove the whole directory once you are done with it.

Here are the steps using Ubuntu:

1. Install Python PIP if you dont already have it: 

echou@ubuntu:~$ sudo apt-get install python-pip
Reading package lists... Done
Building dependency tree    
Reading state information... Done
python-pip is already the newest version.
The following package was automatically installed and is no longer required:
  thunderbird-globalmenu
Use 'apt-get autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 147 not upgraded.
echou@ubuntu:~$

2. Install virtualenv Python package:

echou@ubuntu:~$ sudo pip install virtualenv
Requirement already satisfied (use --upgrade to upgrade): virtualenv in /usr/local/lib/python2.7/dist-packages
Cleaning up...
echou@ubuntu:~$

3. Create the new virtualenv directory:

echou@ubuntu:~$ virtualenv somethingNew
New python executable in somethingNew/bin/python
Installing Setuptools..............................................................................................................................................................................................................................done.
Installing Pip.....................................................................................................................................................................................................................................................................................................................................done.
echou@ubuntu:~$

4. Activate the new virutalenv, now you are in a safe environment: 

echou@ubuntu:~$ source somethingNew/bin/activate
(somethingNew)echou@ubuntu:~$
(somethingNew)echou@ubuntu:~$

5. (Example) Say you want to experiment with Sphinx documentation tool: 

(somethingNew)echou@ubuntu:~$ pip install Sphinx
Downloading/unpacking Sphinx
  Downloading Sphinx-1.1.3.tar.gz (2.6MB): 2.6MB downloaded
  Running setup.py egg_info for package Sphinx
 
<skip>
Successfully installed Sphinx Pygments Jinja2 docutils markupsafe
Cleaning up...
(somethingNew)echou@ubuntu:~$

6. When you are done, simply deactivate: 

(somethingNew)echou@ubuntu:~$ deactivate
echou@ubuntu:~$

7. When you are completely done, delete the whole directory: 

echou@ubuntu:~$ rm -fr somethingNew/
echou@ubuntu:~$ 

8. (Windows) Here is an output under Windows environment:

PS C:\Users\Eric Chou> pip install virtualenv
Downloading/unpacking virtualenv
  Downloading virtualenv-1.10.1.tar.gz (1.3Mb): 1.3Mb downloaded
  Running setup.py egg_info for package virtualenv

    warning: no files found matching '*.egg' under directory 'virtualenv_support'
    warning: no previously-included files matching '*' found under directory 'docs\_templates'
    warning: no previously-included files matching '*' found under directory 'docs\_build'
Installing collected packages: virtualenv
  Running setup.py install for virtualenv

    warning: no files found matching '*.egg' under directory 'virtualenv_support'
    warning: no previously-included files matching '*' found under directory 'docs\_templates'
    warning: no previously-included files matching '*' found under directory 'docs\_build'
    Installing virtualenv-script.py script to C:\Python27\Scripts
    Installing virtualenv.exe script to C:\Python27\Scripts
    Installing virtualenv-2.7-script.py script to C:\Python27\Scripts
    Installing virtualenv-2.7.exe script to C:\Python27\Scripts
Successfully installed virtualenv
Cleaning up...
PS C:\Users\Eric Chou>

* Note, in order to execute the PowerShell script with virtualenv, you need to set the execution policy. Please consult this article before you make this change about security: http://technet.microsoft.com/en-us/library/ee176949.aspx.

PS C:\Windows\system32>  set-executionpolicy Unrestricted

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic. Do you want to change the execution
policy?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y
PS C:\Windows\system32>

PS C:\Users\Eric Chou> .\somethingNew\Scripts\activate
(somethingNew) PS C:\Users\Eric Chou>
(somethingNew) PS C:\Users\Eric Chou>

(somethingNew) PS C:\Users\Eric Chou> pip install Sphinx
Downloading/unpacking Sphinx
  Downloading Sphinx-1.1.3.tar.gz (2.6MB): 2.6MB downloaded

<skip>

* Note. For some reason the deactivate script doesn't work for me, so I simply just close the window.

(somethingNew) PS C:\Users\Eric Chou>
(somethingNew) PS C:\Users\Eric Chou>
(somethingNew) PS C:\Users\Eric Chou> .\somethingNew\Scripts\deactivate.bat
(somethingNew) PS C:\Users\Eric Chou>



If you want to know more, here is the official Virtualenv documentation, http://www.virtualenv.org/en/latest/. 

Happy Coding! 




3 comments: