Skip to content

Create Python Environment

Here, we explain how to use the "virtualenv" command 1 within the Command Line Interface (CLI) to assemble some commonly used python packages for materials science computations into a virtual environment. These packages include the "pymatgen", "pandas" and "atomic simulation environment (ase)" libraries.

Default version of Python is 2.7.5

When a new command line session is created, the active Python version is the system-wide version 2.7.5. Other versions of Python are available for use on an opt-in basis; see the Command Line Environment documentation's section on Python for more details.

Using Virtual Environment

The virtualenv command can be used by following the instructions presented in its official website 1. The installation of a new Python package consists of the following sequence of commands, where we also make use of the pip package manager 2 (already installed by default).

First, we'll create a new virtual environment, as a directory named .virtualenv in the current directory, and then activate it:

1
2
virtualenv .virtualenv
source .virtualenv/bin/activate

At this point the CLI prompt will change to reflect that the virtual environment is active and will look similar to:

1
(.virtualenv) [steven@bohr.exabyte.io:~/cluster-001/tmp]$

Next, let's install the desired package(s). Here we use "pymatgen", further explained below:

1
pip install pymatgen

Check that installation is successful (exit code of zero means everything OK):

1
2
python -c "import pymatgen" && echo $?
0

Now one can execute the scripts requiring the installed package as follows, for example:

1
python script.py

In order to disable the virtual environment and return to the original default command line, enter the following.

1
deactivate

To reactivate an existing virtual environment, source the virtual environment's activate script again:

1
source .virtualenv/bin/activate

Other Versions of Python

As noted above, the default version of Python can be overridden with any of the ways described in the Command Line Environment documentation's section on Python.

Once the chosen version of Python is active, virtual environments can be created as expected and packages installed and code executed against that version. For example, using the Environment Module approach:

1
2
3
4
5
6
7
8
9
module load python/3.8.6
python --version
# Python 3.8.6

virtualenv .virtualenv_py3
source .virtualenv_py3/bin/activate

python --version
# Python 3.8.6

Note that once a Python virtual environment is created, its Python version is permanent and it's not possible to switch to a different version. In that case, a new virtual environment with a different path will need to be created.

Example Python Packages

We support the following python packages in our platform.

Pymatgen

Python Materials Genomics (pymatgen) 3 is a materials analysis code that defines core object representations for crystal structures and molecules, with support for many electronic structure codes. It is currently the core analysis engine powering the Materials Project 4 online database of material structures.

Pandas

Pandas is a python software library for data manipulation and analysis 5. In particular, it offers data structures and operations for manipulating numerical tables and time series.

Atomic Simulation Environment

The Atomic Simulation Environment (ASE) is a set of tools and python modules for setting up, manipulating, running, visualizing and analyzing atomistic simulations 6.