Skip to main content

Docker - Even easier setup of Python

ยท 2 min read
Christophe
Markdown, WSL and Docker lover ~ PHP developer ~ Insatiable curious.

Docker - Even easier setup of Python

Nothing to install, nothing to configure, nothing to create first.

Here is how to use VSCode and Docker to create a ready-to-user Python environment, whatever if you're under Windows, Linux or Mac.

The only prerequisites are: we should have Docker installed on your system, you should have Visual Studio Code and you've installed the Docker for Visual Studio Code addon in VSCode.

If you've these three things, just do this:

  1. Start Visual Studio Code
  2. Press CTRL+SHIFT+P to open the Command Palette. ![Opening the Command Palette]
  3. Select Dev Containers: Add Dev Container Configuration files...
  4. And follow the wizard:
    1. Search for Python
    2. Select the most recent version of Python, right now it's 3.12-bullseye Installing Python 3.12
    3. No need to install additional features, just press Enter
    4. Same for optional files; not needed, just press Enter

This done, VSCode will create a file called .devcontainer/devcontainer.json

VSCode has created the .devcontainer/devcontainer.json file

See bottom right, please click on the Reopen in Container button.

Depending on the speed of your computer and if things were already downloaded, you'll get this screen:

VSCode and his terminal

See the bottom part, a terminal window has been displayed and you've a prompt showing vscode -> /workspaces/python $.

Click in the terminal and type python --version:

Version

Now, for the demo, create a new file called main.py with this line print("Hello from your Python Devcontainer!").

Save the file.

Click in the terminal and type python main.py to execute the script and tadaaaa:

Running the script