9  Installing Python and the opensafely command line interface

9.1 macOS

  • If you have a Mac, the macOS operating system comes with an old-ish version of Python 2.7

  • I recommend installing Python 3 through homebrew

    brew install python
  • When you open Terminal

    • See the contents of PATH with echo $PATH (note use ${PATH} in shell scripts)

    • you should be able to find the python/python3 exectables with the which command

9.2 Windows

  • You have a number of choices where to install Python from

    • Microsoft store, .e.g. Python 3.10 from here
    • Python installer from here
    • Anaconda installer from here
  • Despite not being recommend - it is better for you to add Anaconda/Python to your PATH in the installer options, i.e., check the first box on this screen

    • And in the Python installer check the box adding Python to PATH

  • Open Windows Terminal

    • you can see the contents of PATH in Powershell with $Env:Path

    • and in cmd with echo %PATH%

    • you can see the location of the Python executable in cmd with where python/where python3

  • If you installed Anaconda and you did not add its folders to PATH then you need to install and run opensafely using the Anaconda prompt - you find this as a program under the Start menu

9.3 Installing the opensafely package

  • As long as the python/python3 and pip/pip3 executables are now on your PATH you can simply run in your shell program

    pip install opensafely   
  • This will additionally install its dependency package the cohortextractor package into your Python installation and you should now be able to run opensafely commands such as

    opensafely run run_all