9 Installing Python and the opensafely command line interface
- Environment variables in computer operating systems contain important strings of text
- The
PATH
environment variable is a list of folders which the computer searches in when you type the name of an executable into the command line shell program (usuallyzsh
on macOS,bash
on Ubuntu,cmd
orPowershell
on Windows) - To use the
python
/python3
andpip
/pip3
commands at the shell command line we need to install Python and make sure the folder containing its executable is in ourPATH
environment variable (unless you already know all of this and are going to run Python in Anaconda through the Anaconda Prompt)
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
withecho $PATH
(note use${PATH}
in shell scripts)you should be able to find the
python
/python3
exectables with thewhich
command
9.2 Windows
You have a number of choices where to install Python from
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 screenAnd 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
withecho %PATH%
you can see the location of the Python executable in
cmd
withwhere 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
andpip
/pip3
executables are now on yourPATH
you can simply run in your shell programpip 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