Python: Difference between revisions
No edit summary |
(Added section on listing available wheels) |
||
Line 41: | Line 41: | ||
To exit the virtual environment, simply enter the command <tt>deactivate</tt>: | To exit the virtual environment, simply enter the command <tt>deactivate</tt>: | ||
{{Command|deactivate}} | {{Command|deactivate}} | ||
== Listing available wheels == <!--T:20--> | |||
List currently available wheels with <tt>avail_wheels</tt> command. | |||
By default, it will: | |||
* only show you the '''latest version''' of a specific package; | |||
* only show you versions that are compatible with the python module (if one loaded), otherwise all python versions will be shown; | |||
* only show you versions that are compatible with the CPU architecture that you are currently running on. | |||
To list wheels containing "cdf" (case insensitive) in its name: | |||
{{Command|avail_wheels --name "*cdf*" | |||
name version build python arch | |||
------- --------- ------- -------- ------ | |||
netCDF4 1.4.0 cp27 avx2 | |||
}} | |||
Or to list all available versions: | |||
{{Command|avail_wheels --name "*cdf*" --all_version | |||
name version build python arch | |||
------- --------- ------- -------- ------ | |||
netCDF4 1.4.0 cp27 avx2 | |||
netCDF4 1.3.1 cp36 avx2 | |||
netCDF4 1.3.1 cp35 avx2 | |||
netCDF4 1.3.1 cp27 avx2 | |||
netCDF4 1.2.8 cp27 avx2 | |||
}} | |||
Or to list a specific version: | |||
{{Command|avail_wheels --name "*cdf*" --version 1.3 | |||
name version build python arch | |||
------- --------- ------- -------- ------ | |||
netCDF4 1.3.1 cp36 avx2 | |||
netCDF4 1.3.1 cp35 avx2 | |||
netCDF4 1.3.1 cp27 avx2 | |||
}} | |||
Or to list for a specific version of python: | |||
{{Command|avail_wheels --name "*cdf*" --python 3.6 | |||
name version build python arch | |||
------- --------- ------- -------- ------ | |||
netCDF4 1.3.1 cp36 avx2 | |||
}} | |||
The ''python'' column tell us for which python version the wheel is available, where <tt>cp36</tt> stands for <tt>cpython 3.6</tt>. | |||
===== A few other examples: ===== | |||
* List the wheels for specific architectures : <tt>avail_wheels --arch avx avx2</tt> | |||
* List the wheels specifically for GPU and display only ''name'', ''version'', ''python'' columns: <tt>avail_wheels --column name version python --all_versions --name "*gpu"</tt> | |||
* Display usage and help: <tt>avail_wheels --help</tt> | |||
== Installing modules == <!--T:13--> | == Installing modules == <!--T:13--> |
Revision as of 15:21, 26 July 2018
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:
[name@server ~]$ module avail python
You can then load the version of your choice using module load. For example, to load Python 2.7.14 you can use the command
[name@server ~]$ module load python/2.7.14
In addition to the base Python module, the SciPy package is also available as a module, with the name scipy-stack/2017b. The SciPy module 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:
[name@server ~]$ virtualenv ENV
Once the virtual environment has been created, it must be activated:
[name@server ~]$ source ENV/bin/activate
To exit the virtual environment, simply enter the command deactivate:
[name@server ~]$ deactivate
Listing available wheels
List currently available wheels with avail_wheels command. By default, it will:
- only show you the latest version of a specific package;
- only show you versions that are compatible with the python module (if one loaded), otherwise all python versions will be shown;
- only show you versions that are compatible with the CPU architecture that you are currently running on.
To list wheels containing "cdf" (case insensitive) in its name:
[name@server ~]$ avail_wheels --name "*cdf*"
name version build python arch
------- --------- ------- -------- ------
netCDF4 1.4.0 cp27 avx2
Or to list all available versions:
[name@server ~]$ avail_wheels --name "*cdf*" --all_version
name version build python arch
------- --------- ------- -------- ------
netCDF4 1.4.0 cp27 avx2
netCDF4 1.3.1 cp36 avx2
netCDF4 1.3.1 cp35 avx2
netCDF4 1.3.1 cp27 avx2
netCDF4 1.2.8 cp27 avx2
Or to list a specific version:
[name@server ~]$ avail_wheels --name "*cdf*" --version 1.3
name version build python arch
------- --------- ------- -------- ------
netCDF4 1.3.1 cp36 avx2
netCDF4 1.3.1 cp35 avx2
netCDF4 1.3.1 cp27 avx2
Or to list for a specific version of python:
[name@server ~]$ avail_wheels --name "*cdf*" --python 3.6
name version build python arch
------- --------- ------- -------- ------
netCDF4 1.3.1 cp36 avx2
The python column tell us for which python version the wheel is available, where cp36 stands for cpython 3.6.
A few other examples:
- List the wheels for specific architectures : avail_wheels --arch avx avx2
- List the wheels specifically for GPU and display only name, version, python columns: avail_wheels --column name version python --all_versions --name "*gpu"
- Display usage and help: avail_wheels --help
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:
[name@server ~]$ module load python/2.7.14
We then activate the virtual environment, previously created using the virtualenv command:
[name@server ~]$ source ENV/bin/activate
Finally, we install the latest stable version of Numpy:
[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:
[name@server ~]$ pip install git+git://github.com/numpy/numpy.git
Anaconda
Please see Anaconda.
Jupyter
Please see Jupyter.