You can further customize your codespace by adding container feature, VS Code extensions, VS Code settings, host requirements, and much more.
Let's add the GitHub CLI, extensions to run the python program using VS Code, and a custom script to install some packages when first creating the Codespace.
-
In VS Code, open the Command Palette (
CTRL+SHIFT+P) and select the below command.Codespaces: Add Dev Container Configuration Files...
-
Select the option
Modify your active configuration.... -
In the list of features, search for and select
Pythonfromdevcontainers.- Instead of the defaults, pick
Configure Options. - Leave
Install Toolsset totrue. - Select Python version:
3.10
- Instead of the defaults, pick
-
Navigate to and open the
.devcontainer/devcontainer.jsonfile. -
Verify a new entry similar to the below was added.
"features": { "ghcr.io/devcontainers/features/python:1": { "installTools": true, "version": "3.10" } },
-
In the left navigation, select the Extension tab.
-
Search for
pythonand find entries forPythonandPython Debugger.
-
Right click on each entry and select the
Add to devcontainer.jsonoption.
-
Navigate to and open the
.devcontainer/devcontainer.jsonfile. -
Verify a new entry similar to the below was added.
"customizations": { "vscode": { "extensions": [ "ms-python.python", "ms-python.debugpy" ] } },
The Dev Container specification provides multiple locations to run lifecycle scripts to further customize your Codespace. Let's add the postCreateCommand which runs one time after initial build (or rebuild).
-
Use the VS Code file explorer to create a script file with the below name.
.devcontainer/postCreate.sh
Alternately, run the below terminal command to create it.
touch .devcontainer/postCreate.sh
-
Make the script executable by running the below terminal command.
chmod +x .devcontainer/postCreate.sh
-
Open the
.devcontainer/postCreate.shfile and add the following code, which will install an animation of a steam locomotive.#!/bin/bash sudo apt-get update sudo apt-get install sl echo "export PATH=\$PATH:/usr/games" >> ~/.bashrc echo "export PATH=\$PATH:/usr/games" >> ~/.zshrc
-
Navigate to and open the
.devcontainer/devcontainer.jsonfile. -
Create the
postCreateCommandentry at the same level (top level) asfeatures, andcustomizations."postCreateCommand": ".devcontainer/postCreate.sh"
-
With our new configuration finished, let's commit the changes. Use VS Code's source control tools or the below terminal command.
git add '.devcontainer/devcontainer.json' git add '.devcontainer/postCreate.sh' git commit -m 'feat: Add features, extensions, and postCreate script' git push
-
Open the VS Code Command Palette (
CTRL+Shift+P) and run theCodespaces: Rebuild Containercommand. Select the Rebuild option. A full build is not necessary.
-
Wait a few minutes for the Codespace to rebuild and VS Code to reconnect.
-
With the customizations committed, Mona will begin checking your work. Give her a moment to provide feedback and the next learning steps.
Tip
You can also configure your account to install dotfiles, allowing you to combine personal configurations with the project's configuration.
Now that you've rebuilt the codespace, let's verify the python extension, python version, and custom script were adjusted correctly in the Codespace.
-
Ensure you are in the Codespace.
-
In the left sidebar, click the extensions tab and verify that the Python extensions are installed and enabled.
-
In the left sidebar, select Run and Debug tab and then press the Start Debugging icon. VS Code will open the lower panel and show the run logs.
-
In the lower panel, switch to the TERMINAL tab.
-
Run the following command to show the installed version of Python. Notice the others are not installed.
node --version dotnet --version python --version gh --version
-
Run the following command to show the steam locomotive animation.
sl