PythonVirtualEnv: Difference between revisions
No edit summary |
No edit summary |
||
Line 8: | Line 8: | ||
====Creating the virtual environment (Python 2)==== | ====Creating the virtual environment (Python 2)==== | ||
<pre> | <pre> | ||
$ git clone https://github.com/pypa/virtualenv.git | |||
$ python virtualenv/virtualenv.py env | |||
New python executable in env/bin/python | New python executable in env/bin/python | ||
Installing setuptools.............done. | Installing setuptools.............done. | ||
$ source env/bin/activate | |||
(env) | (env)$ which python | ||
~/env/bin/python | ~/env/bin/python | ||
</pre> | </pre> | ||
Line 37: | Line 37: | ||
<pre> | <pre> | ||
(env) | (env)$ pip install nose | ||
Downloading/unpacking nose | Downloading/unpacking nose | ||
Downloading nose-1.3.3.tar.gz (274Kb): 274Kb downloaded | Downloading nose-1.3.3.tar.gz (274Kb): 274Kb downloaded | ||
Line 46: | Line 46: | ||
Successfully installed nose | Successfully installed nose | ||
Cleaning up... | Cleaning up... | ||
(env) | (env)$ | ||
</pre> | </pre> | ||
====Listing installed Python Modules==== | ====Listing installed Python Modules==== | ||
<pre> | <pre> | ||
(env) | (env)$ pip freeze | ||
nose==1.3.3 | nose==1.3.3 | ||
</pre> | </pre> | ||
Line 57: | Line 57: | ||
'pip' can also be used to remove a module from the environment. | 'pip' can also be used to remove a module from the environment. | ||
<pre> | <pre> | ||
(env) | (env)$ pip uninstall nose | ||
Uninstalling nose: | Uninstalling nose: | ||
/chimerahomes/sabobbin/env/bin/nosetests | /chimerahomes/sabobbin/env/bin/nosetests | ||
Line 71: | Line 71: | ||
To switch between different environments, simply deactivate your current virtual environment, and source the other. | To switch between different environments, simply deactivate your current virtual environment, and source the other. | ||
<pre> | <pre> | ||
(env) | (env)$ which python | ||
~/env/bin/python | ~/env/bin/python | ||
(env) | (env)$ deactivate | ||
$ source env2.7/bin/activate | |||
(env2.7) | (env2.7)$ which python | ||
~/env2.7/bin/python | ~/env2.7/bin/python | ||
</pre> | </pre> | ||
Line 86: | Line 86: | ||
<pre> | <pre> | ||
$ module load Python | |||
$ which python | |||
/usr/local/stow/python-2.7.2/bin/python | /usr/local/stow/python-2.7.2/bin/python | ||
</pre> | </pre> | ||
'''Important:''' virtualenv will include any modules listed in your PYTHONPATH when initializing the virtual environment. To ensure a vanilla environment, it might be a good idea to verify your PYTHONPATH is empty. | '''Important:''' virtualenv will include any modules listed in your PYTHONPATH when initializing the virtual environment. To ensure a vanilla environment, it might be a good idea to verify your PYTHONPATH is empty. | ||
<pre> | <pre> | ||
$ echo $PYTHONPATH | |||
/usr/local/stow/python-commonmodules-2.7.2.0/lib/python2.7/site-packages | /usr/local/stow/python-commonmodules-2.7.2.0/lib/python2.7/site-packages | ||
$ PYTHONPATH='' | |||
</pre> | </pre> | ||
<pre> | <pre> | ||
$ /usr/local/stow/virtualenv-1.5.1/virtualenv.py env2.7 | |||
New python executable in env2.7/bin/python | New python executable in env2.7/bin/python | ||
Installing setuptools....................done. | Installing setuptools....................done. | ||
$ source env2.7/bin/activate | |||
(env2.7) | (env2.7)$ which python | ||
~/env2.7/bin/python | ~/env2.7/bin/python | ||
</pre> | </pre> |
Revision as of 23:54, 7 February 2020
A virtual environment is an isolated working copy of Python, which allows you to work on specific projects without affecting others. It creates an environment that has its own installation directories and that does not share libraries with other virtualenv environments (and optionally doesn’t access the globally-installed libraries either).
While virtualenv should be installed on most UMIACS supported machines, the source can be downloaded from the project's GitHub: https://github.com/pypa/virtualenv
Basic Usage
The following steps outline how to create a virtual environment using the system Python.
Creating the virtual environment (Python 2)
$ git clone https://github.com/pypa/virtualenv.git $ python virtualenv/virtualenv.py env New python executable in env/bin/python Installing setuptools.............done. $ source env/bin/activate (env)$ which python ~/env/bin/python
Creating the virtual environment (Python 3)
Python 3 comes with the virtualenv module built in.
opensub03:~ $ python3 -m venv env
We also suggest the first thing you do is to ensure you have an updated version of pip installed in your environment. Make sure you have sourced your environment, and then run the following.
pip install --upgrade pip
You will notice that once you have created your virtual environment, you will need to use the 'source' command to load it into your environment. In bash and tcsh, the environment can be deactivated by typing deactivate
Installing Python Modules
Once you have created your virtual environment and sourced it, you can install additional modules using the 'pip' command.
(env)$ pip install nose Downloading/unpacking nose Downloading nose-1.3.3.tar.gz (274Kb): 274Kb downloaded Running setup.py egg_info for package nose ...output omitted for brevity... Successfully installed nose Cleaning up... (env)$
Listing installed Python Modules
(env)$ pip freeze nose==1.3.3
Uninstalling Python Modules
'pip' can also be used to remove a module from the environment.
(env)$ pip uninstall nose Uninstalling nose: /chimerahomes/sabobbin/env/bin/nosetests /chimerahomes/sabobbin/env/bin/nosetests-2.4 /chimerahomes/sabobbin/env/lib/python2.4/site-packages/nose /chimerahomes/sabobbin/env/lib/python2.4/site-packages/nose-1.3.3-py2.4.egg-info /chimerahomes/sabobbin/env/man/man1/nosetests.1 Proceed (y/n)? y Successfully uninstalled nose
Switching between virtual environments
To switch between different environments, simply deactivate your current virtual environment, and source the other.
(env)$ which python ~/env/bin/python (env)$ deactivate $ source env2.7/bin/activate (env2.7)$ which python ~/env2.7/bin/python
Using a different python version
To create a virtual environment that uses a version of python that is different then the system default, simply create the virtualenv with your target version of Python. This could be a Python build we provide via Modules, or one you've built yourself. Virtualenv will pick up the first python version it finds in your $PATH, or you can direct it to a specific location with the "--python" flag.
$ module load Python $ which python /usr/local/stow/python-2.7.2/bin/python
Important: virtualenv will include any modules listed in your PYTHONPATH when initializing the virtual environment. To ensure a vanilla environment, it might be a good idea to verify your PYTHONPATH is empty.
$ echo $PYTHONPATH /usr/local/stow/python-commonmodules-2.7.2.0/lib/python2.7/site-packages $ PYTHONPATH=''
$ /usr/local/stow/virtualenv-1.5.1/virtualenv.py env2.7 New python executable in env2.7/bin/python Installing setuptools....................done. $ source env2.7/bin/activate (env2.7)$ which python ~/env2.7/bin/python