Installing Python

Overview

Your workflow should also anticipate some of the following challenges:

  • You are going to work on multiple projects with varying lifespans and potentially conflicting dependencies.
  • The language, some of the Python packages you use, or the packages those packages depend on are going to change while you work.

However, you are responsible for when changes in the language or your dependencies make their way into your projects. Keeping track of which version of Python you’re using is an important first step that will make it easier to share your projects with other people, switch between projects, and debug problems with your code.

How should I install Python?

Warning

A version of Python may come installed with your operating system. Do not delete or modify it.1 Forget it exists.

How you will manage your Python installations depends on whether you manage your own desktop or are working on a server:

Instructions for installing Python are outlined here. Each version of Python on the server will be installed at /opt/python/{PYTHON_VERSION}.

Terminal

ubuntu@ip-172-98-23-134:~$ export PYTHON_VERSION="3.8.13"
ubuntu@ip-172-98-23-134:~$ curl -O https://cdn.rstudio.com/python/ubuntu-2004/pkgs/python-${PYTHON_VERSION}_1_amd64.deb
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                Dload  Upload   Total   Spent    Left  Speed
100 57.0M  100 57.0M    0     0  30.9M      0  0:00:01  0:00:01 --:--:-- 30.8M
ubuntu@ip-172-98-23-134:~$ sudo apt-get install ./python-${PYTHON_VERSION}_1_amd64.deb
Reading package lists... Done
Building dependency tree
Reading state information... Done
Reading state information... Done
Requires the installation of the following packages: libev-dev libev4
interactive high-level object-oriented language
Do you want to install the software package? [y/N]:y
...
Preparing to unpack python-3.8.13_1_amd64.deb ...
Unpacking python-3.8.13 (1) ...
Setting up python-3.8.13 (1) ...
ubuntu@ip-172-98-23-134:~$ export PYTHON_VERSION="3.9.13"
ubuntu@ip-172-98-23-134:~$ curl -O https://cdn.rstudio.com/python/ubuntu-2004/pkgs/python-${PYTHON_VERSION}_1_amd64.deb
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                Dload  Upload   Total   Spent    Left  Speed
100 58.9M  100 58.9M    0     0  36.1M      0  0:00:01  0:00:01 --:--:-- 36.1M
ubuntu@ip-172-98-23-134:~$ sudo apt-get install ./python-${PYTHON_VERSION}_1_amd64.deb
Reading package lists... Done
...
Preparing to unpack python-3.9.13_1_amd64.deb ...
Unpacking python-3.9.13 (1) ...
Setting up python-3.9.13 (1) ...
ubuntu@ip-172-98-23-134:~$ ls /opt/python
3.8.13  3.9.13
ubuntu@ip-172-98-23-134:~$ which python

Users can then use these globally-installed versions of Python to create virtual environments for their projects.

Install pyenv or pyenv for Windows. An easy way to do this is with a package manager2.

Warning

Do not depend a version of Python installed by your package manager.

Your package manager may update Python in a manner incompatible with your data science projects, while other software managed via your package manager may expect the version of Python managed by your package manager to be updated. Using pyenv will help you decouple managing your data science projects from keeping other software on your system up to date.

Once pyenv is installed, install a version of Python.

Note

Python installation may fail due to missing system dependencies. You may have to set environment variables in order to build Python correctly. Additionally, you will have to set PYTHON_CONFIGURE_OPTS="--enable-shared"

Terminal

WDAGUtilityAccount@mvp MINGW64 ~/Documents 
$ pyenv install 3.9.2 3.7.7 
:: [Info] ::  Mirror: https://www.python.org/ftp/python 
:: [Downloading] ::  3.9.2 ... 
:: [Downloading] ::  From https://www.python.org/ftp/python/3.9.2/python-3.9.2-amd64-webinstall.exe 
:: [Downloading] ::  To   C:\Users\WDAGUtilityAccount\scoop\apps\pyenv\current\pyenv-win\install_cache\python-3.9.2-amd64-webinstall.exe 
:: [Installing] ::  3.9.2 ... 
:: [Info] :: completed! 3.9.2 
:: [Downloading] ::  3.7.7 ... 
:: [Downloading] ::  From https://www.python.org/ftp/python/3.7.7/python-3.7.7-amd64-webinstall.exe 
:: [Downloading] ::  To   C:\Users\WDAGUtilityAccount\scoop\apps\pyenv\current\pyenv-win\install_cache\python-3.7.7-amd64-webinstall.exe 
:: [Installing] ::  3.7.7 ... 
:: [Info] :: completed! 3.7.7 

WDAGUtilityAccount@mvp MINGW64 ~/Documents 
$ pyenv rehash 

Calling python if no global version is set will result in a prompt encouraging you to select one.

Terminal

WDAGUtilityAccount@mvp MINGW64 ~/Documents 
$ python --version 
No global python version has been set yet. Please set the global version by typing: 
pyenv global 3.7.2 

WDAGUtilityAccount@mvp MINGW64 ~/Documents 
$ pyenv global 3.9.2 

WDAGUtilityAccount@mvp MINGW64 ~/Documents 
$ python --version 
Python 3.9.2 

You can also set a Python version for use with specific projects:

Terminal

WDAGUtilityAccount@mvp MINGW64 ~/Documents 
$ mkdir older-data-science-project 

WDAGUtilityAccount@mvp MINGW64 ~/Documents 
$ cd older-data-science-project/ 

WDAGUtilityAccount@mvp MINGW64 ~/Documents/older-data-science-project 
$ python --version 
Python 3.9.2 

WDAGUtilityAccount@mvp MINGW64 ~/Documents/older-data-science-project 
$ pyenv local 3.7.7 

WDAGUtilityAccount@mvp MINGW64 ~/Documents/older-data-science-project 
$ python --version 
Python 3.7.7 

WDAGUtilityAccount@mvp MINGW64 ~/Documents/older-data-science-project 
$ cd .. 

WDAGUtilityAccount@mvp MINGW64 ~/Documents 
$ python --version 
Python 3.9.2

Once you’ve installed the versions of Python you need, the next step is to install packages.

Back to top

Footnotes

  1. If you are working in Windows 10, you will want to disable the Python “App execution aliases” as described in this Stack Overflow post.↩︎

  2. webi, Homebrew, scoop, and chocolatey are package managers.↩︎