> ## Documentation Index
> Fetch the complete documentation index at: https://ona.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Python in Gitpod

Gitpod comes with great support for Python built-in. Still, depending on your project, you might want to further optimize the experience.

## Python Project Examples

Before we get started, here are some examples of already-gitpodified repositories!

<div className="overflow-x-auto">
  | Repository                                                                                | Description                                               | Try it                                                                                                                                         |
  | ----------------------------------------------------------------------------------------- | --------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
  | [python-flask-api-tutorial](https://github.com/breatheco-de/python-flask-api-tutorial)    | A step by step Todo List API tutorial with Flask + Python | [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/breatheco-de/python-flask-api-tutorial) |
  | [django-locallibrary-tutorial](https://github.com/gitpod-io/django-locallibrary-tutorial) | An example website written in Django by MDN               | [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/gitpod-io/django-locallibrary-tutorial) |
  | [Gitpod-PyQt](https://github.com/gitpod-io/Gitpod-PyQt)                                   | A PyQt example for Gitpod                                 | [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/gitpod-io/Gitpod-PyQt)                  |
  | [wxPython-example](https://github.com/gitpod-io/wxPython-example)                         | A wxPython example for Gitpod                             | [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/gitpod-io/wxPython-example)             |
  | [template-python-django](https://github.com/gitpod-io/template-python-django)             | A Django example for Gitpod                               | [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/gitpod-io/template-python-django)       |
  | [template-python-flask](https://github.com/gitpod-io/template-python-flask)               | A Flask example for Gitpod                                | [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/gitpod-io/template-python-flask)        |
</div>

## Pandas

Welcome data scientists! This part of the guide will show you how to configure Gitpod for Pandas development.

### Try Pandas in Gitpod

To see a minimal project with Pandas installed and configured, please check out [gitpod-io/Gitpod-Pandas](https://github.com/gitpod-io/Gitpod-Pandas):

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/gitpod-io/Gitpod-Pandas)

### Use Gitpod for Pandas development

Pandas Team has their official docs on how to use Gitpod for Pandas development. Please check out [Pandas Team's Gitpod guide](https://pandas.pydata.org/dev/development/contributing_gitpod.html).

## Jupyter Notebooks in VS Code

For the best experience with Jupyter, please open your Gitpod workspaces in [VS Code Desktop](/classic/user/references/ides-and-editors/vscode).

## Python Versions

Run `pyenv versions` to see which Python versions are pre-installed in Gitpod.

The easiest way to install a new Python version is to use `pyenv install <VERSION>`. For example, suppose you wanted to install Python `3.6.4`, you would run `pyenv install 3.6.4`.

You can switch between Python versions using `pyenv local <VERSION>`. For example, if you wanted to switch to Python version `2.7.17` you would run `pyenv local 2.7.17`. This will create a `.python-version` file that controls which Python version is the default in your project.

If you want to set up a [custom dockerfile](/classic/user/configure/workspaces/workspace-image#use-a-custom-dockerfile) so that you don't have to repeat the process, here's how your `.gitpod.Dockerfile` could look like:

```dockerfile theme={null}
# You could use `gitpod/workspace-full` as well.
FROM gitpod/workspace-python

RUN pyenv install 3.11 \
    && pyenv global 3.11
```

## [Start tasks](/classic/user/configure/workspaces/tasks)

You can start building your project when, or even [before](/classic/user/configure/repositories/prebuilds) you start your Gitpod workspace. Are you using a `requirements.txt` file to manage dependencies? If so, add this to your [.gitpod.yml](/classic/user/references/gitpod-yml) to automatically pre-install all dependencies when starting a workspace:

```yml theme={null}
tasks:
    - init: pip3 install -r requirements.txt
      command: python3 main.py
```

## Linting

You can <a className="no-nowrap" href="https://github.com/palantir/python-language-server#configuration">create a `setup.cfg` or a `pycodestyle.cfg` in the project root</a> and [adjust pycodestyle](http://pycodestyle.pycqa.org/en/latest/intro.html#configuration) rules there like this:

```toml theme={null}
[pycodestyle]
ignore = E226,E302,E41
max-line-length = 160
statistics = True
```

You'll need to refresh the browser in order to update these rule. [Source](https://github.com/gitpod-io/gitpod/issues/640#issuecomment-506622491).

## VS Code Extensions

While the most popular Python VS Code extensions are built into Gitpod, here are a few "nice to have" extensions that you can use as well.

### ARepl for Python

<video controls playsInline autoPlay loop muted className="shadow-medium w-full rounded-xl max-w-3xl mt-x-small" src="https://mintcdn.com/gitpod-13c83c2b/Bzz7ihfWkJmXqMkm/images/docs/AReplExample.webm?fit=max&auto=format&n=Bzz7ihfWkJmXqMkm&q=85&s=bf12eb46037e8f4f36956da14a13ff37" data-path="images/docs/AReplExample.webm" />

ARepl for Python is helpful for constantly checking your code and debugging.
To install this extension for your repository, add the following to your [.gitpod.yml](/classic/user/references/gitpod-yml):

```yml theme={null}
vscode:
    extensions:
        - almenon.arepl
```

### Python Test Explorer

Easily test your python code with the Python Test Explorer.
To add this to your repository add the following to your [.gitpod.yml](/classic/user/references/gitpod-yml)

```yml theme={null}
vscode:
    extensions:
        - littlefoxteam.vscode-python-test-adapter@0.3.16:tZ/6xOSSdKUaq6JCUVkD+A==
```

## GUI Applications with wxPython

To install wxPython to your repository please add the following to your [.gitpod.Dockerfile](/classic/user/configure/workspaces/workspace-image). If you don't have one, simply run [`gp init`](/classic/user/references/gitpod-cli) and commit the two generated files.

```dockerfile theme={null}
# This will pull the official Gitpod `vnc` image
# which has much of what you need to start
FROM gitpod/workspace-full-vnc

USER gitpod

# Install wxPython dependencies
RUN sudo apt-get -q update && \
    sudo DEBIAN_FRONTEND=noninteractive apt-get install -yq freeglut3-dev python3.7-dev libpython3.7-dev libgl1-mesa-dev libglu1-mesa-dev libgstreamer-plugins-base1.0-dev libgtk-3-dev libnotify-dev libsdl2-dev libwebkit2gtk-4.0-dev libxtst-dev libgtk2.0-dev && \
    sudo rm -rf /var/lib/apt/lists/*

# Install wxPython
RUN pip3 install -U -f https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-18.04/ wxPython
```

Here is a corresponding [.gitpod.yml](/classic/user/references/gitpod-yml) example:

```yml theme={null}
image:
    file: .gitpod.Dockerfile

# This will expose all necessary ports needed for your VNC image
ports:
    - port: 6080
      onOpen: open-preview
    - port: 5900
      onOpen: ignore
    - port: 35900
      onOpen: ignore

# This will make it so that on workspace start it will run a file called `app.py`
tasks:
    - command: python3 app.py
```

<br />

We also support other GUI frameworks such as `Kivy` and `PyQt`

<br />

Here are some other examples of Python GUI applications in Gitpod:

<div className="overflow-x-auto">
  | Name                                                              | Framework | Try it                                                                                                                              |
  | ----------------------------------------------------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------- |
  | [Tic-Tac-Toe-GUI](https://github.com/JesterOrNot/Tic-Tac-Toe-GUI) | Kivy      | [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/JesterOrNot/Tic-Tac-Toe-GUI) |
  | [Pong](https://github.com/JesterOrNot/Pong)                       | Kivy      | [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/JesterOrNot/Pong)            |
  | [Gitpod-PyQt](https://github.com/gitpod-io/Gitpod-PyQt)           | PyQt      | [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/gitpod-io/Gitpod-PyQt)       |
</div>

<br />

## Debugging

Here is a quick clip on how to automatically configure debugging for Python!

<video controls playsInline autoPlay loop muted className="shadow-medium w-full rounded-xl max-w-3xl mt-x-small" src="https://mintcdn.com/gitpod-13c83c2b/Bzz7ihfWkJmXqMkm/images/docs/PythonDebug.webm?fit=max&auto=format&n=Bzz7ihfWkJmXqMkm&q=85&s=2fce4ea1db562e1e24ba8075ebbffd89" data-path="images/docs/PythonDebug.webm" />

So, basically in this video we:

1. First, open the Python file that we want to debug
2. Then, go to the debug menu and select "Add Configuration..."
3. Next, in the dropdown choose "Python"
4. Next, choose "Python File" as the debug configuration
5. Finally, start debugging your Python program!

You can also create the Python debug configuration file manually

To start debugging your Python application in Gitpod, please create a new directory called `.theia/`, and inside add a file called `launch.json`, finally, add the following to it:

```json theme={null}
{
	"version": "0.2.0",
	"configurations": [
		{
			"name": "Python: Current File",
			"type": "python",
			"request": "launch",
			"program": "${file}",
			"console": "internalConsole"
		}
	]
}
```

Then, simply open the Python file you want to debug, open the Debug panel (in the left vertical toolbar, click the icon with the crossed-out-spider), and click the green "Run" button.

<br />

To see a basic repository with Python debugging enabled, please check out [gitpod-io/Gitpod-Python-Debug](https://github.com/gitpod-io/Gitpod-Python-Debug):

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/gitpod-io/Gitpod-Python-Debug)

<br />

## Further Reading

* ***[VS Code documentation for Python debugging](https://code.visualstudio.com/python/debugging)*** All the information there should also apply to Gitpod as well.
* ***[Troubleshooting Matplotlib/TK](https://github.com/gitpod-io/gitpod/issues/795)*** Here is how to troubleshoot Matplotlib/TK issues for Python GUI applications.
* ***[Debugging Django](https://community.gitpod.io/t/django-debugging/381/6)*** This is how to debug Django applications in Gitpod.
