Mod:Hunt Research Group/python

From ChemWiki
Revision as of 07:16, 5 December 2019 by Phunt (Talk | contribs) (installing python as a stand alone)

Jump to: navigation, search

Using python on your mac

  • there are many options for using python on your mac (see below)
  • sometimes codes will only work with a specific version of python

using python

  • cd into directory where the py file is
  • to run a python script type
python script.py
  • to exit python type (Control-D)
  • find out what version you have active: "python -V" or "python --version"
  • python -c “commands” [arg] executes commands

installing python as a stand alone

  • there are 2 main versions of python, python2.7 and python3.5 (currently)
you will likely want both
  • use MacPorts to download python
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 could also add an alias to your .bashrc file
alias python37=“/opt/local/bin/python3.7”
  • to ensure the macports version is used
in your $PATH make sure /opt/local/bin occurs before /user/bin
to find out what version you have active: "python -V" or "python --version"

installing python libraries

  • and actually you should go the whole hog with science based libraries
sudo port install py37-numpy py37-matplotlib py37-scipy py37-pandas py37-pip  py37-fortranformat 
  • you can also install with pip
NOTE: "pip" will only install 2.7 version of packages
pip install matplotlib scipy fortranformat
use pip3.x with versions of python
pip3.5 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 .

version active

  • NOTE there can be problems with which version is called, even once python is set using macports
the Mac (Mojave) has a preinstalled version of python in /user/bin
the 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 a prepackaged environments

  • are often favoured by students
  • download mac version of Anaconda mac

useful link http://www.southampton.ac.uk/~fangohr/blog/installation-of-python-spyder-numpy-sympy-scipy-pytest-matplotlib-via-anaconda.html •to check things are working run the code check_python.py ....

  • anaconda can be activated graphically from your Applications folder, click on Anaconda-Navigator.app
  • you want to activate spyder so click on the "launch" button
  • a large window will appear ....
  • spyder is the programming environment