Setting up Python virtualenv: Difference between revisions

From HPCwiki
Jump to navigation Jump to search
Line 23: Line 23:


== deactivating a virtual environment ==
== deactivating a virtual environment ==
Quitting a virtual environment can be done by using the command <code>deactivate</code>, which was loaded using the <code>source</source> command upon activating the virtual environment.
<source lang='bash'>
<source lang='bash'>
deactivate
deactivate
</source>
</source>
=== Make IPython work under virtualenv ==
== Make IPython work under virtualenv ==
source testenv/bin/activate
IPython may not work initially under a virtual environment. It may produce an error message like below:


<code>
<code>
Line 34: Line 35:
                                                                       ^
                                                                       ^
</code>
</code>
This can be resolved by adding a soft link with the name <code>ipython</code> to the <code>bin</code> directory in the virtual environment folder.
This can be resolved by adding a soft link with the name <code>ipython</code> to the <code>bin</code> directory in the virtual environment folder.
<source lang='bash'>
<source lang='bash'>
ln -s /path/to/virtenv/bin/ipython3 /path/to/virtenv/bin/ipython
ln -s /path/to/virtenv/bin/ipython3 /path/to/virtenv/bin/ipython
</source>
</source>

Revision as of 22:40, 24 November 2013

creating a new virtual environment

virtualenv newenv

activating a virtual environment

source newenv/bin/activate

 (newenv)hjm@ubuntu:~$

installing modules on the virtual environment

Installing modules is the same as usual. The difference is that modules are in /path/to/virtenv/lib, which may be living somewhere on your home directory. When working from the virtual environment, the default easy_install will belong to the python version that is currently active. This means that the executable in /path/to/virtenv/bin are in fact the first in the $PATH.

easy_install numpy

Similarly, installing packages from source works exactly the same as usual.

python setup.py install

deactivating a virtual environment

Quitting a virtual environment can be done by using the command deactivate, which was loaded using the source</source> command upon activating the virtual environment.

deactivate

Make IPython work under virtualenv

IPython may not work initially under a virtual environment. It may produce an error message like below:

   File "/usr/bin/ipython", line 11
   print "Could not start qtconsole. Please install ipython-qtconsole"
                                                                     ^

This can be resolved by adding a soft link with the name ipython to the bin directory in the virtual environment folder.

ln -s /path/to/virtenv/bin/ipython3 /path/to/virtenv/bin/ipython