Skip to content

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 responsibile 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-31-1-110:~$ export PYTHON_VERSION="3.7.7"
ubuntu@ip-172-31-1-110:~$ sudo /opt/python/miniconda/bin/conda create --quiet --yes --prefix /opt/python/"${PYTHON_VERSION}" -channel conda-forge python="${PYTHON_VERSION}"
Collecting package metadata (current_repodata.json): ...working... done

...

Executing transaction: ...working... done
ubuntu@ip-172-31-1-110:~$ export PYTHON_VERSION="3.9.2"
ubuntu@ip-172-31-1-110:~$ sudo /opt/python/miniconda/bin/conda create --quiet --yes  -prefix /opt/python/"${PYTHON_VERSION}" --channel conda-forge python="${PYTHON_VERSION}"
Collecting package metadata (current_repodata.json): ...working... done

...

ubuntu@ip-172-31-1-110:~$ ls /opt/python
3.7.7  3.9.2  miniconda  miniconda.sh
ubuntu@ip-172-31-1-110:~$ which python
ubuntu@ip-172-31-1-110:~$
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.

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.


  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. Homebrew, scoop, and chocolatey are package managers.