How to set up a development environment for Python on Ubuntu

Python is an ubiquitous, object-oriented scripting language with an extensive ecosystem. This guide outlines how to install the Python interpreter and developer tooling on Ubuntu.

Installing Python runtime environment

In the Ubuntu package repository, the python3 package always depends on the currently default version of Python (from the 3.x series) in Ubuntu. It is a part of the default system installation, and it ensures that your Python environment is continuously updated as new versions are introduced. The package installs the python3-minimal dependency, which only includes the Python interpreter.

Warning

Do not remove the default system installation of Python (the python3 package), as that would break system tooling.

To get a more useful runtime environment, use the special dependency package, python3-full, which automatically installs the interpreter with the complete class library, support for Python virtual environments (venv), and the basic Python IDE (IDLE). Similarly to python3, the python3-full is a metapackage that always depends on the currently default version of Python in Ubuntu.

sudo apt install -y python3-full

When installed, the /usr/bin/python3 file is a symbolic link always pointing to the currently default version of the Python interpreter binary. For example:

dev@ubuntu:~$ ls -l /usr/bin/python3
lrwxrwxrwx 1 root root 10 Sep 12  2024 /usr/bin/python3 -> python3.12

Note

Python 3 is the default, and Python 2 is no longer officially supported on Ubuntu. As a convenience, consider installing the python-is-python3 package, which provides a symbolic link from /usr/bin/python to /usr/bin/python3.

Installing Python package manager

Numerous Ubuntu system tools make use of the system Python installation. To avoid interfering with this setup and isolate project dependencies, use virtual environments for development and testing.

While the system installation uses Python modules packaged as .deb packages that are available from system repositories, for installing dependencies within Python virtual environments, use the pip Python package manager.

Install with:

sudo apt install -y python3-pip python3-pip-whl

Installing editing and debugging tools

While it is possible to write and edit Python code using any plain-text editor, various integrated development environments (IDEs) offer features to simplify the development process.

Text editors and Language Server Protocol (LSP)

Advanced text editors can be extended using LSP plugins to enhance the user experience with Python.

Vim with Python LSP

A mode-driven text editor with powerful editing features. Combined with an LSP, such as Python LSP Server, it offers code completion, linting, navigation, and others. It can also integrate with other tools, such as Flake8 for error checking.

Install with:

sudo apt install -y vim python3-pylsp flake8
Codium

The freely-licensed binary distribution of Microsoft’s Visual Studio Code. Numerous extensions available from the open-source Open VSX registry provide support for coding with Python. For example, Python and Python Debugger.

Install with:

sudo snap install codium --classic
Visual Studio Code

The popular editor from Microsoft with an extensive range of extensions for Python development, including Python and Python Debugger.

Install with:

sudo snap install code --classic

Integrated development environments

Some of the most common IDEs for Python are:

IDLE (Integrated Development and Learning Environment)

The Python editor and shell is maintained by the Python project and bundled as a dependency of the python3-full package. It is basic but can serve for learning purposes. It includes a simple debugger and a Stack Viewer for tracing errors or exceptions.

Spyder

A community-developed IDE (written in Python) with a special focus on scientific applications.

Install with:

sudo apt install -y spyder
PyCharm

A Python IDE based on the JetBrains platform with open-source and proprietary versions.

Install with:

sudo snap install pycharm-community --classic
PyDev

A Python plugin for the Eclipse IDE.

Install with:

sudo snap install eclipse --classic

From within Eclipse, install PyDev by going to Help ‣ Install New Software… and use http://www.pydev.org/updates for the Work with: field. See Installing in the PyDev Getting started guide.

Apache NetBeans

The netbeansPython plugin adds support for Python to NetBeans (it is based on the python-lsp-server.

Install with:

sudo apt install default-jre
sudo snap install netbeans --classic

From within NetBeans, install netbeansPython by going to Tools ‣ Plugins ‣ Available Plugins ‣ Install.

Linting and code-quality tools

To check and improve code style, formatting, and quality, use, for example, the following tools (while these tools often integrate with text editors and IDEs, they can also be used stand-alone):

Black

The self-styled ‘uncompromising’ code formatter automatically formats Python code to follow PEP 8 guidelines – the official Python style guide.

Install with:

sudo apt install black
Flake8

Code checker. Flake8 is a wrapper for several other tools: PyFlakes, pycodestyle, and the mccabe script.

Install with:

sudo apt install flake8

Testing and debugging tools

Python has the built-in pdb debugger and unittest testing framework, but you can also install additional tools that provide more features and offer more user-friendly controls.

pytest

A flexible testing framework for writing “small, readable” tests.

Install with:

sudo apt install python3-pytest
tox

A tool for automating running tests within Python virtual environments using either the tox.toml or tox.ini configuration files.

Install with:

sudo apt install tox

What next

See the tutorial introducing the use of Python and related tooling: Develop with Python on Ubuntu.