IHE Python course, 2017

Virtual enviroments to cleany separate different versions of python on your computer

T.N.Olsthoorn 2017-03-15

Preface

The information in this notebook was taken verbatim from the websites that are mentioned.

Just scan it through. In the end it's explained how to install virtual enviroments in Anaconda. I would advice to yous that, it's somewhat smoother than the original python methods, and also because it fits better with the Anaconda installation already on your computer.

With virtual environments, there should be no problems with working with different versions of python on the same computer.

Theo Olsthoorn April 04 2017

Tekst from the book a "Hitchikers Guide to Python"

http://python-guide-pt-br.readthedocs.io/en/latest/dev/virtualenvs/

A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the “Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keeps your global site-packages directory clean and manageable.

For example, you can work on a project which requires Django 1.10 while also maintaining a project which requires Django 1.8 (Django is a package to work with the web).

virtualenv

virtualenv is a tool to create isolated Python environments. virtualenv creates a folder which contains all the necessary executables to use the packages that a Python project would need.

Install virtualenv via pip:

This must be done in the cmd window, not from this notebook.


In [3]:
# from terminal or command window
pip install virtualenv


  File "<ipython-input-3-56ebca531cd6>", line 1
    pip install virtualenv
              ^
SyntaxError: invalid syntax

Basic Usage

Create a virtual environment for a project:

This must be done from the cmd windows, not from this notebook


In [ ]:
# from terminal or command window
cd my_project_folder

virtualenv my_project

virtualenv my_project will create a folder in the current directory which will contain the Python executable files, and a copy of the pip library which you can use to install other packages. The name of the virtual environment (in this case, it was my_project) can be anything; omitting the name will place the files in the current directory instead.

The command creates a copy of Python in whichever directory you ran the command in, placing it in a folder named my_project.

You can also use the Python interpreter of your choice (like python2.7).


In [ ]:
# from terminal or command window
virtualenv -p /usr/bin/python2.7 my_project

or change the interpreter globally with an env variable in ~/.bashrc:


In [ ]:
# from terminal or command window
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7

To begin using the virtual environment, it needs to be activated:


In [ ]:
# from terminal or command window
source my_project/bin/activate

The name of the current virtual environment will now appear on the left of the prompt (e.g. (my_project)Your-Computer:your_project UserName$) to let you know that it’s active. From now on, any package that you install using pip will be placed in the my_project folder, isolated from the global Python installation.

Install packages as usual, for example:


In [ ]:
# from terminal or command window
pip install requests

If you are done working in the virtual environment for the moment, you can deactivate it:


In [ ]:
# from terminal or command window
deactivate

This puts you back to the system’s default Python interpreter with all its installed libraries.

To delete a virtual environment, just delete its folder. (In this case, it would be


In [ ]:
# from terminal or command window
rm -rf my_project

After a while, though, you might end up with a lot of virtual environments littered across your system, and its possible you’ll forget their names or where they were placed.

Other Notes

Running virtualenv with the option --no-site-packages will not include the packages that are installed globally. This can be useful for keeping the package list clean in case it needs to be accessed later. [This is the default behavior for virtualenv 1.7 and later.]

In order to keep your environment consistent, it’s a good idea to “freeze” the current state of the environment packages. To do this, run


In [ ]:
# from terminal or command window
pip freeze > requirements.txt

This will create a requirements.txt file, which contains a simple list of all the packages in the current environment, and their respective versions. You can see the list of installed packages without the requirements format using “pip list”. Later it will be easier for a different developer (or you, if you need to re-create the environment) to install the same packages using the same versions:


In [ ]:
# from terminal or command window
pip install -r requirements.txt

This can help ensure consistency across installations, across deployments, and across developers.

Lastly, remember to exclude the virtual environment folder from source control by adding it to the ignore list.

This remark is to prevent that if you have your work in a repository on Github or another site, you don't upload the entire environment to the site, which could be quite large and is useless.

Further info, from the original python tutorial

https://docs.python.org/3/tutorial/venv.html

Virtual Environments and Packages

Introduction

Python applications will often use packages and modules that don’t come as part of the standard library. Applications will sometimes need a specific version of a library, because the application may require that a particular bug has been fixed or the application may be written using an obsolete version of the library’s interface.

This means it may not be possible for one Python installation to meet the requirements of every application. If application A needs version 1.0 of a particular module but application B needs version 2.0, then the requirements are in conflict and installing either version 1.0 or 2.0 will leave one application unable to run.

The solution for this problem is to create a virtual environment, a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.

Different applications can then use different virtual environments. To resolve the earlier example of conflicting requirements, application A can have its own virtual environment with version 1.0 installed while application B has another virtual environment with version 2.0. If application B requires a library be upgraded to version 3.0, this will not affect application A’s environment.

Creating Virtual Environments

The module used to create and manage virtual environments is called venv. venv will usually install the most recent version of Python that you have available. If you have multiple versions of Python on your system, you can select a specific Python version by running python3 or whichever version you want.

To create a virtual environment, decide upon a directory where you want to place it, and run the venv module as a script with the directory path:


In [5]:
# from terminal or command window
python3 -m venv tutorial-env


  File "<ipython-input-5-4cf7a02b14e2>", line 2
    python3 -m venv tutorial-env
                  ^
SyntaxError: invalid syntax

This will create the tutorial-env directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files.

Once you’ve created a virtual environment, you may activate it.

On Windows, run:


In [ ]:
# from terminal or command window
tutorial-env\Scripts\activate.bat

On Unix or MacOS, run:


In [ ]:
# from terminal or command window
source tutorial-env/bin/activate

(This script is written for the bash shell. If you use the csh or fish shells, there are alternate activate.csh and activate.fish scripts you should use instead.)

Activating the virtual environment will change your shell’s prompt to show what virtual environment you’re using, and modify the environment so that running python will get you that particular version and installation of Python. For example:


In [ ]:
# from terminal or command window
source ~/envs/tutorial-env/bin/activate
(tutorial-env) $ python
Python 3.5.1 (default, May  6 2016, 10:59:36)
  ...
>>> import sys
>>> sys.path
['', '/usr/local/lib/python35.zip', ...,
'~/envs/tutorial-env/lib/python3.5/site-packages']
>>>

Virtual environments with Anaconda

If you work essentially in Anaconda as we indeed do, you can also install virtual environments directly with conda or anaconda. This may work a little smoother than the basic procedures shown above. But the difference is really small.


In [6]:
# from terminal or command window
conda update conda


  File "<ipython-input-6-b5d69fa6798b>", line 2
    conda update conda
               ^
SyntaxError: invalid syntax

To search your anaconda distribution (or perhaps even you computer) for the availalbe python version (the ones already installed:


In [ ]:
# from terminal or command window
conda search "^python$"

Create a new vertual environment


In [ ]:
# from terminal or command window
conda create -n yourenvname python=x.x anaconda

Activate the new vertual environment


In [ ]:
# from terminal or command window
source activate yourenvname

Activating a conda environment modifies the PATH and shell variables to point to the specific isolated Python set-up you created. The command prompt will change to indicate which conda environemnt you are currently in by prepending (yourenvname). To see a list of all your environments, use the command


In [ ]:
# from terminal or command window
conda info -e

Install additional Python packages to a virtual environment.

To install additional packages only to your virtual environment, enter the following command where yourenvname is the name of your environemnt, and [package] is the name of the package you wish to install. Failure to specify “-n yourenvname” will install the package to the root Python installation.


In [ ]:
# from terminal or command window
conda install -n yourenvname [package]

Deactivate your virtual environment.

To end a session in the current environment, enter the following. There is no need to specify the envname - which ever is currently active will be deactivated, and the PATH and shell variables will be returned to normal.


In [ ]:
# from terminal or command window
source deactivate

Delete a no longer needed virtual environment

To delete a conda environment, enter the following, where yourenvname is the name of the environment you wish to delete.


In [ ]:
# from terminal or command window
conda remove -n yourenvname -all

The official conda documentation can be found here

https://conda.io/docs/intro.html


In [ ]: