Introduction
This guide shows you how to set up an example Python project in Codespaces, entweder mithilfe der Visual Studio Code-Desktopanwendung oder mithilfe des VS Code-Webclients. Anhand eines Beispiels wird erklärt, wie du dein Projekt in einem Codespace öffnen und eine vordefinierte Entwicklungscontainerkonfiguration hinzufügen und bearbeiten kannst.
Step 1: Open the project in a codespace
-
Go to https://github.com/microsoft/vscode-remote-try-python.
-
Click Use this template, then click Open in a codespace.
When you create a codespace, your project is created on a remote virtual machine that is dedicated to you. By default, the container for your codespace has many languages and runtimes, including Python. It also includes a common set of tools like git, wget, rsync, openssh, and nano.
Du kannst deinen Codespace anpassen, indem du die Anzahl der vCPUs und den RAM anpassst, Dotfiles zum Personalisieren deiner Umgebung hinzufügst oder die installierten Tools und Skripts bearbeitest.
Codespaces verwendet eine Datei namens devcontainer.json
, um den Entwicklungscontainer zu konfigurieren, mit dem du in einem Codespace arbeitest. Jedes Repository kann eine oder mehrere devcontainer.json
-Dateien enthalten, sodass du genau die Entwicklungsumgebung erhältst, die du zum Arbeiten an deinem Code in einem Codespace benötigst.
Beim Starten verwendet Codespaces eine devcontainer.json
-Datei sowie alle davon abhängigen Dateien, aus denen die Entwicklungscontainerkonfiguration besteht, um Tools und Runtimes zu installieren und weitere Einrichtungsaufgaben auszuführen, die für das Projekt benötigt werden. Weitere Informationen findest du unter Einführung in Entwicklungscontainer.
Step 2: Add a dev container configuration
The default development container, or "dev container," for GitHub Codespaces comes with the latest Python version, package managers (pip, Miniconda), and other common tools preinstalled. However, we recommend that you configure your own dev container to include all of the tools and scripts your project needs. This will ensure a fully reproducible environment for all GitHub Codespaces users in your repository.
Wenn du dein Repository so einrichten möchtest, dass ein benutzerdefinierter Entwicklungscontainer verwendet wird, musst du eine oder mehrere devcontainer.json
-Datei(en) erstellen. Du kannst diese entweder aus einer vordefinierten Konfigurationsvorlage in Visual Studio Code hinzufügen oder selbst schreiben. Weitere Informationen zur Konfiguration von Entwicklungscontainern findest du unter Einführung in Entwicklungscontainer.
-
Greife auf die Visual Studio Code Command Palette zu (UMSCHALT+COMMAND+P (Mac)/STRG+UMSCHALT+P (Windows/Linux)) zu, und beginne dann mit der Eingabe „dev container“. Wähle Codespaces: Konfigurationsdateien für Entwicklungscontainer hinzufügen... aus.
-
Start typing
python
and click Python 3 in the list. Other options are available if your project uses particular tools. For example, Python 3 & PostgreSQL. -
Click the latest version of Python 3.
-
A list of additional features is displayed. We'll install Coverage.py, a code coverage tool for Python. To install this tool, type
py
, selectCoverage.py (via pipx)
, then click OK. -
A message is displayed telling you that the dev container configuration file already exists. Click Overwrite.
A
devcontainer.json
file is created and is opened in the editor.
Details of your custom dev container configuration
If you look in the Visual Studio Code Explorer you'll see that a .devcontainer
directory has been added to the root of your project's repository containing the devcontainer.json
file. This is the main configuration file for codespaces created from this repository.
devcontainer.json
The devcontainer.json
file that you have added will contain values for the name
, image
, and features
properties. Some additional properties that you may find useful are included but are commented out.
The file will look similar to this, depending on which image you chose:
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/python
{
"name": "Python 3",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/python:0-3.11-bullseye",
"features": {
"ghcr.io/devcontainers-contrib/features/coverage-py:2": {}
}
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "pip3 install --user -r requirements.txt",
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
- name: You can name your dev container anything you want. A default value is supplied.
- image: The name of an image in a container registry (DockerHub, GitHub Container registry, or Azure Container Registry) that will be used to create the dev container for the codespace.
- features: A list of one or more objects, each of which references one of the available dev container features. Features are self-contained, shareable units of installation code and development container configuration. They provide an easy way to add more tooling, runtime, or library features to your development container. For more information, see "Available Dev Container Features" on the Development Containers website. You can add features by going to the VS Code Command Palette and typing
features
. - forwardPorts: Any ports listed here will be forwarded automatically. For more information, see "Forwarding ports in your codespace."
- postCreateCommand: Use this property to run commands after your codespace is created.
- customizations: This property allows you to customize a specific tool or service when it is used for working in a codespace. For example, you can configure specific settings and extensions for VS Code. For more information, see "Supporting tools and services" on the Development Containers website.
- remoteUser: By default, you’re running as the vscode user, but you can optionally set this to root.
For a complete list of available properties, see the dev containers specification on the Development Containers website.
Additional dev container configuration files
If you are familiar with Docker, you may want to use a Dockerfile, or Docker Compose, to configure your codespace environment, in addition to the devcontainer.json
file. You can do this by adding your Dockerfile
or docker-compose.yml
files alongside the devcontainer.json
file. For more information, see "Using Images, Dockerfiles, and Docker Compose" on the Development Containers website.
Step 3: Modify your devcontainer.json file
With your dev container configuration added and a basic understanding of what everything does, you can now make changes to customize your environment further. In this example, you'll add properties that will:
- Install a package required by the application.
- Install a VS Code extension in this codespace.
-
In the
devcontainer.json
file, add a comma after thefeatures
property, and delete the two commented out lines about features.JSON "features": { "ghcr.io/devcontainers-contrib/features/coverage-py:2": {} }, // Features to add to the dev container. More info: https://containers.dev/features. // "features": {},
-
Uncomment the
postCreateCommand
property.JSON // Use 'postCreateCommand' to run commands after the container is created. "postCreateCommand": "pip3 install --user -r requirements.txt",
-
Uncomment the
customizations
property and edit it as follows to install the "Code Spell Checker" VS Code extension.JSON // Configure tool-specific properties. "customizations": { // Configure properties specific to VS Code. "vscode": { // Add the IDs of extensions you want installed when the container is created. "extensions": [ "streetsidesoftware.code-spell-checker" ] } }
The
devcontainer.json
file should now look similar to this, depending on which image you chose:// For format details, see https://aka.ms/devcontainer.json. For config options, see the // README at: https://github.com/devcontainers/templates/tree/main/src/python { "name": "Python 3", // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile "image": "mcr.microsoft.com/devcontainers/python:0-3.11-bullseye", "features": { "ghcr.io/devcontainers-contrib/features/coverage-py:2": {} }, // Use 'forwardPorts' to make a list of ports inside the container available locally. // "forwardPorts": [], // Use 'postCreateCommand' to run commands after the container is created. "postCreateCommand": "pip3 install --user -r requirements.txt", // Configure tool-specific properties. "customizations": { // Configure properties specific to VS Code. "vscode": { // Add the IDs of extensions you want installed when the container is created. "extensions": [ "streetsidesoftware.code-spell-checker" ] } } // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. // "remoteUser": "root" }
-
Save your changes.
-
Greife auf die VS Code Command Palette zu (UMSCHALT+BEFEHL+P (Mac)/STRG+UMSCHALT+P (Windows/Linux)), und beginne dann mit der Eingabe „rebuild“. Wähle Codespaces: Container neu erstellen aus.
Tipp: Führe gelegentlich eine vollständige Neuerstellung durchführen, um deinen Cache zu löschen und deinen Container mit neuen Images neu zu erstellen. Weitere Informationen findest du unter Durchführen einer vollständigen Neuerstellung eines Containers.
After the dev container is rebuilt, and your codespace becomes available again, the
postCreateCommand
will have been run, installing the package listed in therequirements.txt
file, and the "Code Spell Checker" extension will be available for use.
Step 4: Run your application
In the previous section, you used the postCreateCommand
to install a package for the Flask web framework. You can now use this to run the web application.
-
In the Terminal of your codespace, enter
python -m flask run
. -
When your project starts, you should see a "toast" notification message at the bottom right corner of VS Code, telling you that your application is available on a forwarded port. To view the running application, click Open in Browser.
Step 5: Commit your changes
Nachdem du Änderungen an deinem Codespace vorgenommen hast (neuer Code oder Konfigurationsänderungen), möchtest du deine Änderungen committen. Durch das Committen von Änderungen an dein Repository wird sichergestellt, dass jeder Benutzer, der einen Codespace aus diesem Repository erstellt, die gleiche Konfiguration verwendet. Dies bedeutet auch, dass alle von dir ausgeführten Anpassungen, z. B. das Hinzufügen von VS Code-Erweiterungen, für alle Benutzer angezeigt werden.
Weitere Informationen findest du unter Verwenden der Quellcodeverwaltung in deinem Codespace.
Next steps
You should now be able to add a custom dev container configuration to your own Python project.