PythonVirtualEnv: Difference between revisions

From UMIACS
Jump to navigation Jump to search
No edit summary
No edit summary
 
(17 intermediate revisions by 3 users not shown)
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).


While virtualenv should be installed on <I>most</I> 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. Please note that Python virtual environments are not relocatable after being installed, so consider the install location carefully. Home directories may have [[Quota | quotas]] that are not suited for very large environments.


==Basic Usage==
====Creating the virtual environment on Python 3====
The following steps outline how to create a virtual environment using the system Python.
Python 3 on [[RHEL | Red Hat Enterprise Linux (RHEL)]] comes with the virtualenv module built in. If you are running [[Ubuntu]] on a UMIACS-supported workstation, please contact [[HelpDesk | UMIACS Staff]] to install venv on your machine. In the below example, <tt>env</tt> is the virtual environment name. You can substitute in any other name of your choosing.
<pre>
$ python3 -m venv env
</pre>


====Creating the virtual environment (Python 2)====
====Creating the virtual environment on Python 2 (deprecated)====
<pre>
<pre>
-bash-3.2$ git clone https://github.com/pypa/virtualenv.git
$ git clone https://github.com/pypa/virtualenv.git
-bash-3.2$ python virtualenv/virtualenv.py env
$ 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.
-bash-3.2$ source env/bin/activate
(env)-bash-3.2$ which python
~/env/bin/python
</pre>
</pre>


====Creating the virtual environment (Python 3)====
====Activating the VirtualEnv====
Python 3 comes with the virtualenv module built in.
You will need to use the 'source' command to load it into your shell environment every time you open a new shell.
<pre>
<pre>
opensub03:~ $ python3 -m venv env
$ 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.
{{Note|<b>Note:</b> tcsh/csh shell users should run <code>source env/bin/activate.csh</code> instead.}}
 
We also suggest the first thing you do is to ensure you have an updated version of pip, setuptools and wheel installed in your environment.  Make sure you have sourced your environment, and then run the following:


<pre>
<pre>
pip install --upgrade pip
$ pip install --upgrade pip setuptools wheel
</pre>
</pre>


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 <code>deactivate</code>
In bash and tcsh, the environment can be deactivated by typing <code>deactivate</code>


====Installing Python Modules====
====Installing Python Modules====
Line 37: Line 38:


<pre>
<pre>
(env)-bash-3.2$ pip install nose
$ pip install nose
Downloading/unpacking nose
Collecting nose
   Downloading nose-1.3.3.tar.gz (274Kb): 274Kb downloaded
   Downloading nose-1.3.7-py3-none-any.whl (154 kB)
  Running setup.py egg_info for package nose
    |████████████████████████████████| 154 kB 12.5 MB/s
 
Installing collected packages: nose
...output omitted for brevity...
Successfully installed nose-1.3.7
 
Successfully installed nose
Cleaning up...
(env)-bash-3.2$
</pre>
</pre>


====Listing installed Python Modules====
====Listing installed Python Modules====
<pre>
<pre>
(env)-bash-3.2$ pip freeze
(env)$ pip freeze
nose==1.3.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>
(env)-bash-3.2$ pip uninstall nose
$ pip uninstall nose
Uninstalling nose:
Found existing installation: nose 1.3.7
   /chimerahomes/sabobbin/env/bin/nosetests
Uninstalling nose-1.3.7:
  /chimerahomes/sabobbin/env/bin/nosetests-2.4
   Would remove:
  /chimerahomes/sabobbin/env/lib/python2.4/site-packages/nose
    /nfshomes/username/env/bin/nosetests
  /chimerahomes/sabobbin/env/lib/python2.4/site-packages/nose-1.3.3-py2.4.egg-info
    /nfshomes/username/env/bin/nosetests-3.4
  /chimerahomes/sabobbin/env/man/man1/nosetests.1
    /nfshomes/username/env/lib/python3.7/site-packages/nose-1.3.7.dist-info/*
    /nfshomes/username/env/lib/python3.7/site-packages/nose/*
    /nfshomes/username/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 the other.
To switch between different environments, simply deactivate your current virtual environment, and source another.
<pre>
<pre>
(env)-bash-3.2$ which python
(env)$ which python
~/env/bin/python
~/env/bin/python


(env)-bash-3.2$ deactivate  
(env)$ deactivate  


-bash-3.2$ source env2.7/bin/activate
$ source my-other-env/bin/activate


(env2.7)-bash-3.2$ which python
(my-other-env)$ which python
~/env2.7/bin/python
~/my-other-env/bin/python
</pre>
</pre>


==Using a different python version==
==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.
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>
-bash-3.2$ module load Python
$ module load Python3


-bash-3.2$ which python
$ which python3
/usr/local/stow/python-2.7.2/bin/python
/opt/local/stow/Python3-3.8.1/bin/python3
</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>
-bash-3.2$ echo $PYTHONPATH
$ echo $PYTHONPATH
/usr/local/stow/python-commonmodules-2.7.2.0/lib/python2.7/site-packages


-bash-3.2$ PYTHONPATH=''
</pre>
</pre>
<pre>
<pre>
-bash-3.2$ /usr/local/stow/virtualenv-1.5.1/virtualenv.py env2.7
$ python3 -m venv env36
New python executable in env2.7/bin/python
Installing setuptools....................done.


-bash-3.2$ source env2.7/bin/activate
$ source env36/bin/activate
(env2.7)-bash-3.2$ which python
(env36) $ which python
~/env2.7/bin/python
~/env36/bin/python
</pre>
</pre>

Latest revision as of 13:45, 23 October 2024

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. Please note that Python virtual environments are not relocatable after being installed, so consider the install location carefully. Home directories may have quotas that are not suited for very large environments.

Creating the virtual environment on Python 3

Python 3 on Red Hat Enterprise Linux (RHEL) comes with the virtualenv module built in. If you are running Ubuntu on a UMIACS-supported workstation, please contact UMIACS Staff to install venv on your machine. In the below example, env is the virtual environment name. You can substitute in any other name of your choosing.

$ 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 every time you open a new shell.

$ source env/bin/activate
Exclamation-point.png Note: tcsh/csh shell users should run source env/bin/activate.csh instead.

We also suggest the first thing you do is to ensure you have an updated version of pip, setuptools and wheel installed in your environment. Make sure you have sourced your environment, and then run the following:

$ pip install --upgrade pip setuptools wheel

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/username/env/bin/nosetests
    /nfshomes/username/env/bin/nosetests-3.4
    /nfshomes/username/env/lib/python3.7/site-packages/nose-1.3.7.dist-info/*
    /nfshomes/username/env/lib/python3.7/site-packages/nose/*
    /nfshomes/username/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