Python development guidelines

GitLab requires Python as a dependency for reStructuredText markup rendering. It requires Python 3.

Installation

There are several ways of installing Python on your system. To be able to use the same version we use in production, we suggest you use pyenv. It works and behaves similarly to its counterpart in the Ruby world: rbenv.

macOS

To install pyenv on macOS, you can use Homebrew with:

brew install pyenv

Windows

pyenv does not officially support Windows and does not work in Windows outside the Windows Subsystem for Linux. If you are a Windows user, you can use pyenv-win.

To install pyenv-win on Windows, run the following PowerShell command:

Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; &"./install-pyenv-win.ps1"

Learn more about pyenv-win.

Linux

To install pyenv on Linux, you can run the command below:

curl "https://pyenv.run" | bash

Alternatively, you may find pyenv available as a system package via your distribution’s package manager.

You can read more about it in the pyenv prerequisites.

Shell integration

Pyenv installation adds required changes to Bash. If you use a different shell, check for any additional steps required for it.

For Fish, you can install a plugin for Fisher:

fisher add fisherman/pyenv

Or for Oh My Fish:

omf install pyenv

Dependency management

While GitLab doesn’t directly contain any Python scripts, because we depend on Python to render reStructuredText markup, we need to keep track on dependencies on the main project level, so we can run that on our development machines.

Recently, an equivalent to the Gemfile and the Bundler project has been introduced to Python: Pipfile and Pipenv.

A Pipfile with the dependencies now exists in the root folder. To install them, run:

pipenv install

Running this command installs both the required Python version as well as required pip dependencies.

Use instructions

To run any Python code under the Pipenv environment, you need to first start a virtualenv based on the dependencies of the application. With Pipenv, this is a simple as running:

pipenv shell

After running that command, you can run GitLab on the same shell and it uses the Python and dependencies installed from the pipenv install command.

Learning resources

If you are new to Python or looking to refresh your knowledge, this section provides variours materials for learning the language.

  1. Python Cheatsheet A comprehensive reference covering essential Python syntax, built-in functions, and useful libraries. This is ideal for both beginners and experienced users who want a quick, organized summary of Python’s key features.

  2. A Whirlwind Tour of Python (Jupyter Notebook)
    A fast-paced introduction to Python fundamentals, tailored especially for data science practitioners but works well for everyone who wants to get just the basic understanding of the language. This is a Jupiter Notebook which makes this guide an interactive resource as well as a good introduction to Jupiter Notebook itself.

  3. 100-page Python Intro
    Brief guide provides a straightforward introduction to Python, covering all the essentials needed to start programming effectively. It’s a beginner-friendly option that covers everything from syntax to debugging and testing.

  4. Learn X in Y Minutes: Python
    A very brief, high-level introduction cuts directly to the core syntax and features of Python, making it a valuable quick start for developers transitioning to Python.