Jump to content

Mod:Hunt Research Group/python

From ChemWiki

Python is an object-orientated programming language. Python is currently available as python 2 and python 3. Python 3 is recommended as future development will focus on this language, however, there are some programmes that are still using (and will only work with) python 2. It is also good to be aware that some programmes may only work for a specific version of python (as with most software).

Installation

Macs will have an inbuilt version of python (usually a version of 2). You can also install python as a stand-alone package or using a distribution such as Anaconda. It is recommended to use one of the latter two routes to install and run python as they allow for better environment management. Routes to install are given below.

Anaconda

Anaconda is an open-source distribution of the python (and R) language. The package manager Conda is included as part of the Anaconda distribution and enables easy package, dependency and environment management. Anaconda downloads a pre-packaged version of python alongside a large number of useful packages.

To install Anaconda:

  • Anaconda is available to download from here.
  • Select your OS system
  • Download the Python 3.# version
  • Follow the installer instructions from the download

Thorough installation instructions and troubleshooting are available here

Following install, there will automatically be some lines added to your .bash_profile (or .zsh_profile). You can leave these or optionally move the lines to the bottom of the corresponding .*rc file instead for execution on startup. The lines added should append the Anaconda python location to your path and make it the default.

If this does not work then check whether the anaconda python location is on your path and make sure that it takes precedence above other python locations by editing your path. For example adding to the start of your path list:

PATH="/Users/${USER}/anaconda3/bin:${PATH}

Installing libraries

As mentioned above Anaconda automatically installs a large number of packages. There are a large number of other packages available through the Anaconda Distribution (using conda) or the Anaconda Cloud. You can use Anaconda through either a GUI available through the Navigator programme (from your Applications folder, click on Anaconda-Navigator.app) that will be installed or through the terminal using conda. Check the section below for useful/popular libraries.

Anaconda allows for easy environment management. An environment is a container for a collection of packages and settings that you can implement and switch between. In terms of reproducibility, it is good practice in computational research to know the packages and versions used to reproduce specific analysis etc.

You can make multiple environments and switch between them. The conda documentation probably gives the best guide, here to environment management. For example, you may want to make a second conda environment with python 2 installed.

Stand-alone python Install

You can install python as a stand-alone from most package managers (MacPorts, Homebrew, Conda (use miniconda if you don't want to install the full Anaconda distribution), etc.).

Installation using MacPorts

To install python using MacPorts use the following commands:

sudo port search --name --line --glob 'python*'
sudo port installed 'python*'
sudo port select --list python
sudo port install python37
port select --set python python37
sudo port install python 27
port select --set python2 python27

You can also add an alias to your .bashrc file

alias python37=“/opt/local/bin/python3.7”

To ensure that the MacPorts python version is used in your path make sure that /opt/local/bin occurs before /user/bin in your ${PATH}.

Installing libraries using MacPorts

Check the section below for useful/popular libraries. To install libraries using MacPorts you can use the general command (using the py3 set alias):

sudo port install py37-[packagename]

Where packagename is the package that you wish to install. For example:

sudo port install py37-numpy py37-matplotlib py37-scipy py37-pandas py37-pip  py37-fortranformat 

Installing libraries using pip

NOTE: The command "pip" will only install 2.7 version of packages. To install a package type:

pip install matplotlib scipy fortranformat

To install packages for python 3, use pip3.x with versions of python:

pip3 install matplotlib scipy fortranformat
  • possible issue found in 2018
pip3 --version delivers 3.5 and not 3.7
which pip shows /opt/local BUT pip3 shows /usr/local
so pip3 is from the wrong installation of python
sudo port select --set pip pip27 works BUT sudo port select --set pip3 pip37 fails!
so force the use of python3: sudo -H python3 -m pip install .

Active Version Issues

  • NOTE there can be problems with which version is called, even once python is set using MacPorts
Mac (Mojave) has a preinstalled version of python in /user/bin
Mac (Sierra) has a preinstalled version of python in /user/local/bin
python.org installs in /Library/Frameworks/Python.framework/Versions/3.5/bin
MacPorts installs in /opt/local/bin
to find out which version is being used type "which python"
to find out the version of python used "python -V":or "python --version"
to ensure the MacPorts version is used in your $PATH make sure /opt/local/bin occurs before /user/bin
sometimes other initiating files add things to your path, so key an eye on this variable
choose in what order to search the relevant directories

for example

::export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
::export PATH=$PATH:/Users/$USER/bin
::[tricia@clove]/Users/tricia $ echo $PATH
::/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/tricia/bin
::[tricia@clove]/Users/tricia $

Using Python

Whichever method you have installed python by, make sure that the version you want is the default one to be called.

  • To find out which default python interpreter you are using type: which python
  • To find which version you have active type: "python -V" or "python --version"

To execute a python script:

  • cd into the directory where the python script is located.
  • To run the python script (e.g. script.py):
python script.py

You can also interactively run python from the terminal:

  • Type python in the CL. The terminal should switch to an interactive python session. It will also notify you which python version you are using at the start.
  • Use python to your heart's content
  • To exit type (Control-D)or exit()

Useful Libraries

  • NumPy: A scientific computing library that contains a wide range of linear algebra tools. NumPy home
  • Pandas: A python data analysis and data structure library. Pandas home
  • Matplotlib: A 2D plotting library (has some 3D plotting functionality). Matplotlib home
  • Seaborn: A statistical data visualisation library based on matplotlib. Seaborn home

Useful Links/Misc