Setting up Python virtualenv: Difference between revisions

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


== activating a virtual environment ==
== activating a virtual environment ==
Once the environment is created, each time the environment needs to be activated, the following command needs to be issued:
<source lang='bash'>
<source lang='bash'>
source newenv/bin/activate
source newenv/bin/activate
</source>
</source>
This assumes that the folder that contains the virtual environment documents (in this case called <code>newenv</code>), is in the present working directory.
When working on the virtual environment, the virtual environment name will be between brackets in front of the <code>user-host-prompt</code> string.
<code>
<code>
   (newenv)hjm@ubuntu:~$
   (newenv)user@host:~$
</code>
</code>
== installing modules on the virtual environment ==
== installing modules on the virtual environment ==
Installing modules is the same as usual. The difference is that modules are in <code>/path/to/virtenv/lib</code>, which may be living somewhere on your home directory. When working from the virtual environment, the default <code>easy_install</code> will belong to the python version that is currently active. This means that the executable in <code>/path/to/virtenv/bin</code> are in fact the first in the <code>$PATH</code>.
Installing modules is the same as usual. The difference is that modules are in <code>/path/to/virtenv/lib</code>, which may be living somewhere on your home directory. When working from the virtual environment, the default <code>easy_install</code> will belong to the python version that is currently active. This means that the executable in <code>/path/to/virtenv/bin</code> are in fact the first in the <code>$PATH</code>.

Revision as of 21:53, 24 November 2013

With many Python packages available, which are often in conflict or requiring different versions depending on application, installing and controlling packages and versions is not always easy. In addition, so many packages are often used only occasionally, that it is questionable whether a system administrator of a centralized server system or a High Performance Compute (HPC) infrastructure can be expected to resolve all issues posed by users of the infrastructure. Even on a local system with full administrative rights managing versions, dependencies, and package collisions is often very difficult. The solution is to use a virtual environment, in which a specific set of packages can then be installed. As many different virtual environments can be created, and used side-by-side, as is necessary.

creating a new virtual environment

It is assumed that the appropriate virtualenv executable for the Python version of choice is installed. A new virtual environment, in this case called newenv is created like so: <source lang='bash'> virtualenv newenv </source> When the new environment is created, one will see a message similar to this:

 New python executable in newenv/bin/python3
 Also creating executable in newenv/bin/python
 Installing Setuptools.........................................................................done.
 Installing Pip.....................................................................................done.

activating a virtual environment

Once the environment is created, each time the environment needs to be activated, the following command needs to be issued: <source lang='bash'> source newenv/bin/activate </source> This assumes that the folder that contains the virtual environment documents (in this case called newenv), is in the present working directory. When working on the virtual environment, the virtual environment name will be between brackets in front of the user-host-prompt string.

 (newenv)user@host:~$

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. <source lang='bash'> easy_install numpy </source> Similarly, installing packages from source works exactly the same as usual. <source lang='bash'> python setup.py install </source>

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. <source lang='bash'> deactivate </source>

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. <source lang='bash'> ln -s /path/to/virtenv/bin/ipython3 /path/to/virtenv/bin/ipython </source>