Python: Difference between revisions

From Alliance Doc
Jump to navigation Jump to search
m (Clarify the meaning of exp1 in the example of activating a virtual environment.)
No edit summary
Line 21: Line 21:
{{Command|module load python/2.7.13}}
{{Command|module load python/2.7.13}}


In addition to the base Python module, the [https://www.scipy.org/ SciPy] package is also available as a module, with the name <tt>python27-scipy-stack</tt> and
In addition to the base Python module, the [https://www.scipy.org/ SciPy] package is also available as a module, with the name <tt>python27-scipy-stack</tt> or
<tt>python35-scipy-stack</tt> depending on which Python module you have loaded. You can see a list of the packages that come with the SciPy stack by consulting
<tt>python35-scipy-stack</tt> depending on which Python module you have loaded. It includes NumPy, Pandas, and several other popular Python modules. You can see the complete list and details at [https://www.scipy.org/stackspec.html Scipy.org].
this [https://www.scipy.org/stackspec.html page].


== Creating and using a virtual environment == <!--T:7-->
== Creating and using a virtual environment == <!--T:7-->

Revision as of 14:13, 26 July 2017

Other languages:

Description

Python is an interpreted programming language with a design philosophy stressing the readability of code. Its syntax is simple and expressive. Python has an extensive, easy-to-use library of standard modules.

The capabilities of Python can be extended with modules developed by third parties. In general, to simplify operations, it is left up to individual users and groups to install these third-party modules in their own directories. However, most systems offer several versions of Python as well as tools to help you install the third-party modules that you need.

The following sections discuss the Python interpreter, and how to install and use modules.

Loading an interpreter

To discover the versions of Python available:

Question.png
[name@server ~]$ module avail python

You can then load the version of your choice using module load. For example, to load Python 2.7.13 you can use the command

Question.png
[name@server ~]$ module load python/2.7.13

In addition to the base Python module, the SciPy package is also available as a module, with the name python27-scipy-stack or python35-scipy-stack depending on which Python module you have loaded. It includes NumPy, Pandas, and several other popular Python modules. You can see the complete list and details at Scipy.org.

Creating and using a virtual environment

With each version of Python, we provide the tool virtualenv. This tool allows users to create virtual environments within which you can easily install Python modules. These environments allow one to install many versions of the same module, for example, or to compartmentalize a Python installation according to the needs of a specific project.

To create a virtual environment, enter the following command, where ENV is the name of the empty directory containing your environment:

Question.png
[name@server ~]$ virtualenv ENV

Once the virtual environment has been created, it must be activated:

Question.png
[name@server ~]$ source ENV/bin/activate

To exit the virtual environment, simply enter the command deactivate:

Question.png
[name@server ~]$ deactivate

Installing modules

Once you have a virtual environment loaded, you will be able to run the pip command. This command takes care of compiling and installing most of Python modules and their dependencies. A comprehensive index of Python packages can be found at PyPI.

All of pip's commands are explained in detail in the user guide. We will cover only the most important commands and use the Numpy module as an example.

We first load the Python interpreter:

Question.png
[name@server ~]$ module load python/2.7.13

We then activate the virtual environment (presuming that exp1 is the name of a directory - ENV in the previous section, previously set up with the virtualenv command):

Question.png
[name@server ~]$ source exp1/bin/activate

Finally, we install the latest stable version of Numpy:

Question.png
[name@server ~]$ pip install numpy

If we wanted to install the development version of Numpy, we can also give a link toward its Git repository:

Question.png
[name@server ~]$ pip install git+git://github.com/numpy/numpy.git