Python Development Environment
This page describes in more detail, how to easily setup your Python development environment on Raspbian.
Check out our main Python page for more interesting links and ideas.
Please note: At the Raspberry Valley makerspace, we will use Python 3 only (Python 2 is coming to an end anyway and essentials libraries are upgraded). Together with Python 3, we always use Virtual Environments. This guide works for your recent Raspbian, as well as other systems; Ubuntu or even Windows Subsystem for Linux.
We will start with a fresh install of debian. As of this writing, it has Python 2.x with Pip, Python 3.x with Pip. Please note down the current versions.
python --version pip --version python3 --version pip3 --version
At any time, you might also simply check available versions of Python by running the below list printout:
ls -l /usr/bin/python*
You might want to also check an exhaustive description of other ways to find Python on your system at linuxconfig.org
Note: on the Windows Subsystem for Linux Debian, only Python 3.x is installed, together with pip3. Recently, we have also seen a debialn WSL, which was installed with no Python at all. In that case, you need to run the Python installation first:
Install Python (skip, if Python is present already)
sudo apt update sudo apt install python3 python3-pip
Follow individual steps below to setup your environment.
pip are already installed, see previous note.
- update and upgrade
sudo apt update && sudo apt full-upgrade
- Setup basic dependencies: We are building a development environment, so the below will come in handy.
sudo apt install build-essential cmake pkg-config
Setup Virtual Environments
Setup Virtual Environments. Learn more in this excellent Python Virtual Environments Primer. With Python 3.x, most of your work is done; the venv module from the standard library is installed.
If you develop both on Raspbian and Windows, you can follow this guide to setup virtual environments on Windows. For Raspbian, follow the guide below.
- Setup a directory, where you will store your environments / code. In our examples, we will use
python-envsas the development directory, but of course you can apply your naming.
mkdir python-envs && cd python-envs
- Setup virtualenvwrapper to manage your virtual environments. The tooling of venv of course works perfectly, but you want to make your development more convenient, right?
pip3 install virtualenvwrapper
Tip: The location of virtualenvwrapper may differ, depending on how you setup the tool. If you ran it as we did, your installation is located in
~/.local/bin/. If you ran system-wide (with sudo), then the path to the wrapper is
/usr/local/bin. Locate the correct location before going further - you might want to run the following to find the correct location:
Tip: on Windows, virtualenvwrapper won't work. Check the Windows specific version - virtualenvwrapper-win instead.
- Update your console settings. You can update either the
~/.bashrcfile, or the
~/.profilewith the settings below. Learn more about the difference between those 2 files here.
First, open up
sudo nano ~/.bashrc
Attach the following to the end of the file (this will enable your sessions to setup correct paths etc.). You can read more about the configuration at the official pages here.
export WORKON_HOME=$HOME/.virtualenvs export PROJECT_HOME=./python-envs export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 export VIRTUALENVWRAPPER_VIRTUALENV=~/.local/bin/virtualenv source ~/.local/bin/virtualenvwrapper.sh
- Test the configuration: Try creating a dummy virtual environment to validate all works well
At this point in time, your new environment is created and activated. you can see the active environment on the command-line, in brackets.
Working with the environment boils down to basically using to core commands listed below. If you wish to know more, check the reference here.
- mkvirtualenv: used to create a new environment. We recommend to create one for every project you have
- workon: select the environment you wish to work with
- deactivate: unselect the environment you were using
- lsvirtualenv: list available virtual environments
- rmvirtualenv: remove an environment you no longer use
Below is an example of what can be your workflow with virtual environments.
The above guide got you prepared to start a project. Now how you handle your development is entirely up to you. We think it is a good idea to have a simple workflow always on mind. For example:
- Setup a virtual environment for your project and activate it using
- Setup your git repository somewhere (github, gitlab, bitbucket, your own server ....) and synchronize it. We find it a good habit to setup the git repository meta-date right away: project description, icon etc.
- Add a .gitignore, README, CHANGELOG to your project
- in your activated virtual environment - install pre-requisites. What we install is the following:
- Edit your code one feature at a time, then commit with a good descriptive comment
- once you are done editing, deactivate your virtual environment using
- If you are using Visual Studio Code, your need to bind to the virtual environment you are working on. To do so, activate the Palette -
Ctrl+Shift+P, and use the feature
Python: Select Interpreter.
pipenv: If you would like to take your development environment to yet another level, consider also using the tool pipenv. It is a dependency manager for Python projects.
Our Python related articles: