-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-09-installing-python-and-os.qmd
67 lines (59 loc) · 3.22 KB
/
01-09-installing-python-and-os.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Installing Python and the opensafely command line interface
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, fig.align = "center")
```
- 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 (usually `zsh` on macOS, `bash` on Ubuntu, `cmd` or `Powershell` on Windows)
- To use the `python`/`python3` and `pip`/`pip3` commands at the shell command line we need to install Python and make sure the folder containing its executable is in our `PATH` environment variable (unless you already know all of this and are going to run Python in Anaconda through the Anaconda Prompt)
## 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](https://brew.sh/)
```bash
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
```{r}
knitr::include_graphics("img/mac-terminal-zsh-which-python.png")
```
## Windows
- You have a number of choices where to install Python from
- Microsoft store, .e.g. Python 3.10 from [here](https://www.microsoft.com/store/productId/9PJPW5LDXLZ5)
- Python installer from [here](https://www.python.org/downloads/)
- Anaconda installer from [here](https://www.anaconda.com/products/individual)
- 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
```{r, fig.align="center"}
knitr::include_graphics("img/anaconda-win-install-options.png")
```
- And in the Python installer check the box adding Python to `PATH`
```{r}
knitr::include_graphics("img/install-python-add-to-path.png")
```
- Open Windows Terminal
- you can see the contents of `PATH` in Powershell with `$Env:Path`
```{r}
knitr::include_graphics("img/windows-powershell-path.png")
```
- and in `cmd` with `echo %PATH%`
```{r}
knitr::include_graphics("img/windows-cmd-path.png")
```
- you can see the location of the Python executable in `cmd` with `where python`/`where python3`
```{r}
knitr::include_graphics("img/windows-cmd-where-python3.png")
```
- 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
```{r}
knitr::include_graphics("img/anaconda-prompt-start-menu-win11.png")
knitr::include_graphics("img/anaconda-prompt-cmd.png")
```
## 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
```bash
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
```bash
opensafely run run_all
```