PythonVirtualEnv: Difference between revisions
No edit summary |
|||
Line 1: | Line 1: | ||
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). | 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). | ||
==Basic Usage== | ==Basic Usage== | ||
The following steps outline how to create a virtual environment using the system Python. | The following steps outline how to create a virtual environment using the system Python. | ||
====Creating the virtual environment | ====Creating the virtual environment on Python 3==== | ||
Python 3 comes with the virtualenv module built in. | |||
<pre> | |||
$ python3 -m venv env | |||
</pre> | |||
====Creating the virtual environment on Python 2 (deprecated)==== | |||
<pre> | <pre> | ||
$ git clone https://github.com/pypa/virtualenv.git | $ git clone https://github.com/pypa/virtualenv.git | ||
Line 12: | Line 16: | ||
New python executable in env/bin/python | New python executable in env/bin/python | ||
Installing setuptools.............done. | Installing setuptools.............done. | ||
</pre> | </pre> | ||
==== | ====Activating the VirtualEnv==== | ||
You will need to use the 'source' command to load it into your shell environment. | |||
<pre> | <pre> | ||
$ | $ source env/bin/activate | ||
</pre> | </pre> | ||
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 | 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: | ||
<pre> | <pre> | ||
Line 31: | Line 30: | ||
</pre> | </pre> | ||
In bash and tcsh, the environment can be deactivated by typing <code>deactivate</code> | |||
====Installing Python Modules==== | ====Installing Python Modules==== | ||
Line 37: | Line 36: | ||
<pre> | <pre> | ||
$ pip install nose | |||
Collecting nose | |||
Downloading nose-1.3. | Downloading nose-1.3.7-py3-none-any.whl (154 kB) | ||
|████████████████████████████████| 154 kB 12.5 MB/s | |||
Installing collected packages: nose | |||
Successfully installed nose-1.3.7 | |||
Successfully installed nose | |||
</pre> | </pre> | ||
Line 52: | Line 47: | ||
<pre> | <pre> | ||
(env)$ pip freeze | (env)$ pip freeze | ||
nose==1.3. | nose==1.3.7 | ||
</pre> | </pre> | ||
====Uninstalling Python Modules==== | ====Uninstalling Python Modules==== | ||
'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> | ||
$ pip uninstall nose | |||
Uninstalling nose: | Found existing installation: nose 1.3.7 | ||
/ | Uninstalling nose-1.3.7: | ||
Would remove: | |||
/nfshomes/liam/env/bin/nosetests | |||
/nfshomes/liam/env/bin/nosetests-3.4 | |||
/nfshomes/liam/env/lib/python3.7/site-packages/nose-1.3.7.dist-info/* | |||
/nfshomes/liam/env/lib/python3.7/site-packages/nose/* | |||
/nfshomes/liam/env/man/man1/nosetests.1 | |||
Proceed (y/n)? y | Proceed (y/n)? y | ||
Successfully uninstalled nose | Successfully uninstalled nose-1.3.7 | ||
</pre> | </pre> | ||
===Switching between virtual environments=== | ===Switching between virtual environments=== | ||
To switch between different environments, simply deactivate your current virtual environment, and source | To switch between different environments, simply deactivate your current virtual environment, and source another. | ||
<pre> | <pre> | ||
(env)$ which python | (env)$ which python | ||
Line 76: | Line 74: | ||
(env)$ deactivate | (env)$ deactivate | ||
$ source | $ source my-other-env/bin/activate | ||
( | (my-other-env)$ which python | ||
~/ | ~/my-other-env/bin/python | ||
</pre> | </pre> | ||
==Using a different | ==Using a different Python version== | ||
To create a virtual environment that uses a version of python that is different | To create a virtual environment that uses a version of python that is different than the system default, 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. | ||
<pre> | <pre> | ||
$ module load | $ module load Python3 | ||
$ which | $ which python3 | ||
/ | /opt/local/stow/Python3-3.8.1/bin/python3 | ||
</pre> | </pre> | ||
'''Important:''' | |||
'''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 | $ echo $PYTHONPATH | ||
</pre> | </pre> | ||
<pre> | <pre> | ||
$ | $ python3 -m venv env36 | ||
$ source | $ source env36/bin/activate | ||
( | (env36) $ which python | ||
~/ | ~/env36/bin/python | ||
</pre> | </pre> |
Revision as of 15:54, 10 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).
Basic Usage
The following steps outline how to create a virtual environment using the system Python.
Creating the virtual environment on Python 3
Python 3 comes with the virtualenv module built in.
$ python3 -m venv env
Creating the virtual environment on Python 2 (deprecated)
$ git clone https://github.com/pypa/virtualenv.git $ python virtualenv/virtualenv.py env New python executable in env/bin/python Installing setuptools.............done.
Activating the VirtualEnv
You will need to use the 'source' command to load it into your shell environment.
$ source env/bin/activate
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
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.
$ pip install nose Collecting nose Downloading nose-1.3.7-py3-none-any.whl (154 kB) |████████████████████████████████| 154 kB 12.5 MB/s Installing collected packages: nose Successfully installed nose-1.3.7
Listing installed Python Modules
(env)$ pip freeze nose==1.3.7
Uninstalling Python Modules
'pip' can also be used to remove a module from the environment.
$ pip uninstall nose Found existing installation: nose 1.3.7 Uninstalling nose-1.3.7: Would remove: /nfshomes/liam/env/bin/nosetests /nfshomes/liam/env/bin/nosetests-3.4 /nfshomes/liam/env/lib/python3.7/site-packages/nose-1.3.7.dist-info/* /nfshomes/liam/env/lib/python3.7/site-packages/nose/* /nfshomes/liam/env/man/man1/nosetests.1 Proceed (y/n)? y Successfully uninstalled nose-1.3.7
Switching between virtual environments
To switch between different environments, simply deactivate your current virtual environment, and source another.
(env)$ which python ~/env/bin/python (env)$ deactivate $ source my-other-env/bin/activate (my-other-env)$ which python ~/my-other-env/bin/python
Using a different Python version
To create a virtual environment that uses a version of python that is different than the system default, 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 Python3 $ which python3 /opt/local/stow/Python3-3.8.1/bin/python3
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
$ python3 -m venv env36 $ source env36/bin/activate (env36) $ which python ~/env36/bin/python