set up your development environment for multiple versions of python using pyenv and venv
Startup Guide for Python
Python is a way to get your hands on powerful development tools, but in its own ways needs to be managed.
tl;dr
Install the latest release from python.org for general purpose usage, or manage multiple versions of Python using pyenv (which can be installed view homebrew). Install packages to individual projects with separate virtual environments using venv.
common commands
While Python has it's own interactive interpreter, a common way to interact with python will take place through a shell) terminal.
version number
python3 -v # or use python3 --version
install pips
pip install -r requirements.txt
create a virtual environment using venv
python3 -m venv env
activate an environment
source env/bin/activate
or you can run a script using that environment
env/bin/python3 script.py
freeze pips
pip freeze > requirements.txt
Continue below for a more in depth discussion on setting up a development environment for Python.
basic installation
If you're using Python for a single project, you might be able to get away with just installing the latest release from python.org (the official webpage for the language). If you're using Python regularly, you've probably encountered situations where different projects require different versions of Python itself. Whether it's code breaking changes being made to the next version, or maybe a pip
is just out of date, managing Python versions on your machine globally becomes increasingly important.
pip
The most useful thing about Python is the immense repository of packages that are available for use. A package/dependency is referred to as a pip
(Preferred Installer Programs from the Python Package Index). When using pips we can very easily integrate powerful libraries into our projects by installing them via the command line:
pip install <package-name> # python must be installed for this to work
You can list the pips that have been installed using pip list
, and you can even write those pips to text file as a quick reference when setting up the project on another computer using the freeze and install commands.
# write to a file pip freeze > requirements.txt
And now when our project is moved to a new machine, we can grab the dependencies by simply running:
# install list of pips pip install -r requirements.txt
venv
A common practice is to organize our pips by project, since we may not need the same packages for separate projects. This is done through a virtual environment. While there are a ton of packages and methods to handle this task, my preferred method is the venv
module, native to Python since version 3.3.
Once python is installed you can create a new environment by running:
python3 -m venv env # creates a folder named 'env'
After creating our env directory, you can activate that python environment by running:
source env/bin/activate
When you install packages via pip, these dependencies will be stored in the env
directory. The one issue with venv
is that it can only create environments in the same Python version as the one it was created with. In order to use multiple versions of Python on your machine, we can use pyenv.
pyenv
The documentation for pyenv
recommends using homebrew for installation. So after installing homebrew
you can run:
brew install pyenv
To install a version of python:
pyenv install 3.10.4
You can get a list of all the available versions of python using
pyenv install -l
Switch between different versions of python that are installed using the global
command.
pyenv global 3.10
After properly configuring pyenv
we can now create virtual environments for any version of python we've installed by switching to that installation and running python3 -m venv <environment-name>
in the directory where we want the environment to exist.
cool & helpful links
dataquest - installing python on mac
Published on: November 27, 2024