Python
Introduction
Python is a programming language. See www.python.org for more
information. Python has two flavours a 2.X and a 3.X series. The 2.X
series became unsupported at the end of 2020 and should not be used
for new code. For portable future-proof code, use the 3.X
series. However, the default version of python on many of our
operating systems is from the 2.X series, althought this is
changing. Don't use the default system python, use one of the many
python 3 programs installed.
A good way to get started working with python is to use the Anaconda Python
distribution, this has over 200 packages preinstalled and you can
add many others using the 'conda install package_name' command which
does not require sudo or root privileges. If you want to install
Anaconda python on an Astrophysics machine, you don't need any
elevated rights, just follow the web page here.
Documentation
See docs.python.org.
Initialisation
There are many versions of python installed on the Astrophysics
systems, and which one you get is determined by your PATH variable.
Here are some of the ways you can use python.
Anaconda Python
You install this yourself in your home directory. There are
instructions here.
The quick and dirty method to use Anaconda python is to change your PATH variable to put the Anaconda bin directory first in your PATH. You can do this as shown below:
Substitute your username for XXXXX below.
bash/sh shells:
export PATH=/home/XXXXX/anaconda3/bin:$PATH
tcsh/csh shells:
setenv PATH /home/XXXXX/anaconda3/bin:$PATH
To save on typing, make an alias in your .bashrc file to run the
command above.
bash:
alias ana3='export PATH=/home/XXXXX/anaconda3/bin:$PATH'
tcsh:
alias ana3 'setenv PATH /home/XXXXX/anaconda3/bin:$PATH'
If you are using anaconda python in a virtual environment, you must
activate that environment using the conda command, eg
conda activate my_environment
you need to have created this environment and defined the python version beforehand.
python3 command
You should be able to access a recent python 3 using the python3
command, so just use python3 instead of python. This version won't
have as many packages installed as Anaconda python, but you can
install any missing ones using the pip or pip3 commands. For example
pip3 install astropy --user
Python module
Some of our more powerful servers have the module command installed,
you use this as below:
To see what is available, type:
module available
and you should see something like the below.
------------------------ /usr/share/Modules/modulefiles ------------------------
dot module-git module-info modules null use.own
------------------------------- /opt/modulefiles -------------------------------
python/3.9.3_x86_64
to use this version of python type:
module load python/3.9.3_x86_64
Verifying the Python Version
You can check this using the 'which' command as below:
which python
/usr/bin/python
You can check which version of python that is by using the '-V' option
as below:
python -V
Python 2.6.6
Version
Lots of versions. Unless you have a very good reason not to, new code
should use Python 3 syntax.
- /usr/bin/python is the system version (usually an old python 2)
- /usr/bin/python3 is a recent python 3
- /home/XXXXX/anaconda3/bin will be your own personal Anaconda python, if you have installed it.
Other versions can be found in /usr/local/Python-*
Also check
out the parallelised Intel Python here.
Testing
Put the following in a text file called pythontest.py and make it
executable with chmod +x pythontest.phy, then run it as
./pythontest.py.
#!/usr/bin/python3
print ("Hello from Python")
The version of python named on the first line is the version that will
be used to run the code that follows it.
Usage
Python can be run interactively after typing the name of whichever
version of python you want, type your commands at the '>>>' prompt or
you can write a script where the first line is
#!/usr/blah/python_version. Don't forget to make the script executable
with a chmod +x script_name.
You can also run python in a notebook with 'live code' such as Jupyter Notebook.
Related Software
Pip and Conda
If you are using anaconda python, use 'conda install' to install
packages if you are using a system python, then you can use 'pip
install --user' to install packages, neither command requires sudo or
root access.
pip install --user astropy
or if you are using a 3.6.X version of Python
pip36 install --user matplotlib
or to upgrade a package to the latest available version:
pip36 install --user matplotlib --upgrade
You should also add ~/.local/bin to your PATH variable as that is
where pip puts executable programs.
Add this to the end of your .cshrc file, using your own username.
setenv PATH /homeb/yourusername/.local/bin:${PATH}
Rhys Morris
Last modified: Monday, 22 March 2021