Setting Up Python on Your Computer
Introduction
In this post, we are going to look at how python can be set up on your local computer. This article suggests you:
- Install
pyenv
to install your desired python version. - Setup local environments in a single folder.
- Setup development tools, linters, formatters, etc via apps like
pipx
.
You can read more in detail below.
Packages to Install
Before we start, I use brew to install any software further discussed in this article. You can have it installed from their website.
I will be using several software packages in this article. You are going to be installing the below apps to your local machine.
- pyenv, a tool that allows you to install multiple versions of python.
- pipx, install python CLI based apps in isolated environments.
- virtualenv, an enhanced version of venv module for creating virtual environments.
Steps
Install Python
There are many articles on the internet about why you shouldn't use your local python that is shipped with your machine. In short, using local python might break your computer, these versions are also generally old, like python 2, etc. Instead, you can use pyenv or alternatives to installing python. pyenv
is a "Simple Python Version Management" tool, that manages the version of python in the system. Some advantages of python version managers:
- Allows you to install multiple python versions in a single machine.
- Seach commands from multiple python versions which become useful in testing.
Install python's build dependencies first. Whenever pyenv compiles the wanted version of python, sometimes you may get errors regarding "unmet dependencies". In order to not get this, you will need this step.
brew install openssl readline sqlite3 xz zlib
Download and install pyenv
with brew.
brew install pyenv
Add to ~/.zshrc
or ~/.zprofile
as mentioned in Basic GitHub Checkout of the pyenv repository.
echo 'eval "$(pyenv init --path)"' >> ~/.zprofile
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
By default, pyenv installs to ~/.pyenv
directory, but you can change it by setting up PYENV_ROOT
environment variable.
pyenv root
# ~/.pyenv
Once pyenv is installed, you can now install python.
pyenv install 3.7.3
You can have multiple python versions installed on the same computer and changing different python versions be easy with pyenv global {python version}
command.
pyenv versions
# system
# 3.5.9
# 3.7.3
# 3.8.3
# * 3.8.5 (set by /Users/me/.pyenv/version)
python --version
# Python 3.8.5
pyenv global 3.7.3
python --version
# Python 3.7.3
You now have python installed and have an option to install more python versions. You can switch between python versions, and set project-specific versions, and many more. All of your python installs comes from ~/.pynev
folder without interrupting system python.
Install pipx
Pipx is a great way to install python-based command-line applications. It isolates the app in a virtual environment so that it doesn't bother your project and gives you access to the command line app directly.
You can install it via brew. Installation is pretty straightforward but to make sure you can follow instructions on pipx's installation guide..
brew install pipx
pipx ensurepath
After this, you can follow instructions on pipx completions
for shellcode completion. With pipx, it's super easy to install and manage python packages. Pipx by default installs environments into $HOME/.local/pipx/venvs
location, but with an environment variable (PIPX_HOME
), you can move these environments into your preference.
Pipx has a dependency on python and therefore brew installs python along with pipx. Now you have multiple python installs on your computer, your system python, pyenv python versions, and brew python. When pipx creates an environment, brew python version will be used. This will break your pipx installs every time you do brew upgrade && brew update
. Although pipx developers provide reinstall-all command to reinstall all packages, I believe setting a default python version will make life easier. In order to prevent this from happening, you should set the environment variable PIPX_DEFAULT_PYTHON
to a version you installed via pyenv.
# set python executable as your choise of preference
export PIPX_DEFAULT_PYTHON="$HOME/.pyenv/versions/3.7.3/bin/python"
Setup Environments
You will keep all of our environments in one single folder. Keeping all environments in one folder gives you many advantages, including:
- Multiple projects can use the same environment.
- When working with IDEs.
- You won't populate your computer with
.venv
folders in every project. - You can use all your environments as kernels in Jupyter Notebook.
To set this up, we will use any virtualenv tool, for this case, it will be virtualenv
.
pipx install virtualenv
Create an environment with your system python version in a single location, ~/.virtualenvs/
.
virtualenv ~/.virtualenvs/NewEnv37
While creating the virtual environment, you can also change the python version via --python
argument.
# setting python path
virtualenv ~/.virtualenvs/NewEnv37 --python=`which python`
# same as above.
virtualenv ~/.virtualenvs/NewEnv37 --python=/Users/me/.pyenv/shims/python
If you don't want to define this variable every time, virtualenv's python executable can be changed with an environment variable as well.
export VIRTUALENV_PYTHON="$HOME/.pyenv/versions/3.7.3/bin/python"
Above command sets the following configuration file ~/.virtualenvs/NewEnv37/pyvenv.cfg
for the new environment NewEnv37
.
home = /Users/me/.pyenv/versions/3.7.3
implementation = CPython
version_info = 3.7.3.final.0
virtualenv = 20.0.35
include-system-site-packages = false
base-prefix = /Users/me/.pyenv/versions/3.7.3
base-exec-prefix = /Users/me/.pyenv/versions/3.7.3
base-executable = /Users/me/.pyenv/versions/3.7.3/bin/python
With your virtual environments ready, you can use them as kernels to Jupyter Notebooks and set up your IDE to find these as an interpreter.
// VSCODE settings.json file.
{
...
"python.defaultInterpreterPath": "~/.pyenv/versions/3.7.3/envs/default-3.7/bin/python",
"python.venvFolders": [
".virtualenvs",
"/path/to/virtualenvs"
],
|
Development Tools like Linters, Formatters, etc.
Development tools such as linters, formatters have their own dependencies, and installing them into your project's environment will make your requirements file overloaded. Also, you shouldn't install these tools every time a new project starts, you should have them available to your IDE. Therefore, managing these applications with an app like pipx
will take the responsibility away from you. Pipx will ensure that these apps are in your path and findable in your terminal.
pipx install autopep8
pipx install black
pipx install pylint
pipx install flake8
Conclusion
In this article, I outlined how you can install python, virtual environments, and development tools. Using tools like pyenv
, pipx
gives you the flexibility to set up your local development environment easier and safer without interrupting any of your computer's locale python. You can switch between your python versions via pyenv global
function, and set up project-specific python versions.
All apps you have installed with pipx will be available in your path and therefore you can use these in your IDE or somewhere else to increase your productivity.