Home >Backend Development >Python Tutorial >Python Development in VSCode Using Devcontainer
In this guide, we'll walk you through the steps to set up and use the devcontainer-python-template for Python development in a Docker-based development environment using Visual Studio Code (VSCode). This template allows you to get up and running quickly with a Python project inside a devcontainer.
Instead of cloning the repository directly, you'll create your own repository based on the template, so you can push your changes back to your own repository.
This will create a new repository on your GitHub account, initialized with the contents of the devcontainer-python-template.
git clone git@github.com:jdevto/python-flask-web-app.git cd python-flask-web-app
It's always a best practice to create a branch and work from it.
git clone git@github.com:jdevto/python-flask-web-app.git cd python-flask-web-app
git branch dev git checkout dev
Open the Terminal:
Running the postCreateCommand from devcontainer.json... [7382 ms] Start: Run in container: /bin/sh -c .devcontainer/scripts/postCreate.sh Linux 726a838654fd 6.8.0-49-generic #49~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Nov 6 17:42:15 UTC 2 x86_64 GNU/Linux Done. Press any key to close the terminal.
Before proceeding, ensure Python is working correctly inside the devcontainer:
root ➜ /workspaces/python-flask-web-app (main) $
python --version
The template includes a simple test script located at hello/main.py. Run this script to validate the setup and ensure Python is executing correctly:
Python 3.12.7
python hello/main.py
Once you confirm Python is working and the test script runs successfully:
Hello, Devcontainer!
rm -rf hello
Create the Project Directory
# python-flask-web-app A simple demo showcasing a Python Flask web application running in a VSCode development container. This setup is designed to provide an isolated, portable, and consistent environment for Python development.
Set Up a Virtual Environment
mkdir flask-web-app cd flask-web-app
Install Flask
python -m venv venv source venv/bin/activate
pip install flask
Create the App
git clone git@github.com:jdevto/python-flask-web-app.git cd python-flask-web-app
git branch dev git checkout dev
Run the App
Running the postCreateCommand from devcontainer.json... [7382 ms] Start: Run in container: /bin/sh -c .devcontainer/scripts/postCreate.sh Linux 726a838654fd 6.8.0-49-generic #49~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Nov 6 17:42:15 UTC 2 x86_64 GNU/Linux Done. Press any key to close the terminal.
root ➜ /workspaces/python-flask-web-app (main) $
Expand the Project Structure
As the app evolves, add a directory structure that supports templates, static files, and tests.
Create the following structure:
python --version
Python 3.12.7
Update base.html file with the following contents:
python hello/main.py
Run the Flask App Again
Run the app again to test the updated version.
Hello, Devcontainer!
Open a browser and navigate to http://127.0.0.1:5000.
You should see something like this:
rm -rf hello
Stop the Flask Server
Exit out of the Development Environment
# python-flask-web-app A simple demo showcasing a Python Flask web application running in a VSCode development container. This setup is designed to provide an isolated, portable, and consistent environment for Python development.
Navigate to Your Repository on GitHub
Go to the "Pull Requests" Tab
Create a New Pull Request
Select Branches for the PR
Open Pull Request Creation
Review the Changes
Add a Title and Description
Provide a concise and descriptive title (e.g., feat: update template dev).
Add a description with any necessary details (e.g., reasons for changes, references to issues, etc.).
Finalize the Pull Request
Review and Approve the PR
Merge the Pull Request
The above is the detailed content of Python Development in VSCode Using Devcontainer. For more information, please follow other related articles on the PHP Chinese website!