This procedure walks you through creating a new Python project in Visual Studio Code, complete with a virtual environment so the project’s packages stay isolated from the rest of the system.
🧰 Prerequisites
Install all three before continuing. The steps below assume they are already set up.
- Python installed
- Visual Studio Code installed
- Python extension installed
📋 Steps
-
Open Visual Studio Code.
-
Open the Explorer Tab and click Open Folder.
-
Create a new folder or choose an existing one.
-
Open the Command Palette with
Ctrl+Shift+P(Windows/Linux) orCmd+Shift+P(Mac). -
Type
Python: Create Environmentand select it from the list. -
Select Venv. This creates a virtual environment, a private space where the project keeps its own copy of Python and its packages, separate from every other projects.
-
Select the Python interpreter to use. This is the version of Python that will run the code. If only one is installed, pick that one.
-
Confirm the name
.venvfor the virtual environment. -
Select Skip Package installation.
-
Wait for VS Code to finish creating the environment.
-
When finished, a
.venvfolder will appear in the project, and the new environment is selected automatically. -
In the Explorer Tab, click the New File icon next to the folder name.
-
Name the file
main.pyand pressEnter. -
Open
main.pyand add a single line:print("Hello, World!"). -
Save the file
- Windows/Linux:
Ctrl+S - Mac:
Cmd+S
- Windows/Linux:
🎯 Expected Outcome
- The project folder contains a
.venvfolder. - The project folder contains a
main.pyfile. main.pycontains a single line:print("Hello, World!").
⚠️ Mistakes
No folder open before creating the environment
The virtual environment and files are created inside whichever folder is open in VS Code. Without a folder open, there is nowhere for them to live, and VS Code cannot create the environment. Always open a folder first via File → Open Folder before starting.
Python not installed
The Python extension helps you write Python, but it does not include Python itself. Without Python installed, the Create Environment step has no interpreter to choose and cannot finish.
Skipping the virtual environment
It is possible to create main.py without an environment, and a single line will still run. But installing packages later without a virtual environment mixes them into the system-wide Python, where they can clash with other projects. Creating the environment first, as a habit, avoids that from the start.
📚 Resources
- Getting Started with Python in VS Code (external link), official tutorial
- Python environments in VS Code (external link), official guide
- Python downloads (external link), official site