Currently the posts are filtered by: Python
Reset this filter to see all posts.


Creating your personal Python environment using virtualenv

Often you are working on a computer where you do not have the user privileges to install custom Python modules (or software in general). Or you do not want to mess with the operating systems Python environment. Then, sometimes you just want (or have to) install python modules with a specific version, e.g. to get the exact behaviour as on a given server. This is where virtualenv comes in. Virtualenv is a tool to create virtual Python environments, that are exactly like a complete Python installation and are therefore fully customizable but do not need root privileges.

In this tutorial, I assume you already have a computer where you have Python up and running, and that you know how to run it in your shell. This is e.g. the case for all Ubuntu based systems.

Now let's make use of virtualenv


Using Gildas/Class from Python

As many people have been asking me on how to use Gildas/Class from within Python, I'll try to give a quick tour here. You will need to have Class installed including the Pyclass extension.

Before you start the Python script you need to initialize Gildas in the shell. Make also sure it is in your $PYTHONPATH. On my local PC using a bash shell where Gildas is installed in $GILDAS_DIR that is

source $GILDAS_DIR/
export PYTHONPATH=$PYTHONPATH:$GILAS_DIR/gildas-exe-mar18b/x86_64-ubuntu14.04-gfortran/python/

The example above is for the version of Class tested in March 2018 - you might need to change the path for your installed version accordingly. Check if Class is running as usual and that indeed you find the Gildas-path in your $PYTHONPATH.

Then you can execute the Python script or go into iPython. Here is a minimal example:

import pygildas, pyclass
# if you want your program to catch PygildasErrors:
from pgutils import PygildasError
# you are good to go from here, but to access stuff easier, I prefer
# to assign the two most commonly used objects to individual 
# variables for easy access
# get the gildas command line interpreter to issue commands quickly
sic = pyclass.comm
# get the pyclass.gdict object for easy Gildas variable access
g = pyclass.gdict
# just execute commands as you would within Class
# you just do not have the display - use matplotlib ;)
sic('SIC MESSAGE GLOBAL ON') # or OFF for less noise
sic('file in "myData.apex"')
# ...

To access Gildas variables we use the pyclass.gdict (the object we assigned to g). Unfortunately