Creating a new Python project in Visual Studio Code primarily involves setting up a dedicated folder for your code, configuring a virtual environment, and installing essential extensions to streamline your development workflow.
The Recommended Approach: Project Folder Setup
The most robust way to begin a new Python project is by creating a dedicated folder on your system and then opening it as a workspace in VS Code. This method provides a clear structure for all your project files and configurations.
Step-by-Step Project Folder Creation
- Create a New Folder: On your operating system (Windows, macOS, or Linux), create an empty folder. Give it a meaningful name that reflects your project (e.g.,
my_awesome_project
). - Open in VS Code:
- Launch Visual Studio Code.
- Go to
File > Open Folder...
from the top menu and select the folder you just created. - Alternatively, you can drag and drop the project folder directly onto the VS Code icon or its welcome screen.
Once opened, you will see your project folder listed in the Explorer sidebar (the leftmost panel).
- Add Your First Python File:
- With your project folder open, locate the "New File" icon (a document with a plus sign) next to your project folder's name in the Explorer sidebar. Click it.
- Type a name for your Python file, such as
main.py
, and press Enter. This creates your first Python script within your new project.
Quick Start: Creating a New Python File Directly
For quick tests, single scripts, or if you prefer a less structured start, you can create a Python file directly within VS Code without first opening a dedicated project folder. This is useful for experimenting but less ideal for full-fledged projects that require multiple files and dependencies.
- From the Welcome Page: On the VS Code Welcome page, select
New File
, then choosePython file
from the options. - Via the Menu: Navigate to
File > New File
from the top menu. This creates an unsaved, unassigned file. You must then save it with a.py
extension (e.g.,script.py
) to activate Python features.
If you already have a workspace folder open in VS Code, you can add new files or folders directly into your existing project using the Explorer view, as described in Step 3 of the recommended approach.
Essential Setup: Virtual Environments
For any serious Python project, utilizing a virtual environment is a critical best practice. It isolates your project's dependencies, ensuring that packages installed for one project don't conflict with those of another or your system's global Python installation.
Creating and Activating a Virtual Environment
With your project folder open in VS Code:
- Open the Integrated Terminal: Go to
Terminal > New Terminal
from the top menu, or use the shortcutCtrl +
` (backtick). - Create the Virtual Environment: In the terminal, run one of the following commands. The
venv
module is built into Python 3.python -m venv .venv
(Recommended for most systems)python3 -m venv .venv
(Commonly used on macOS/Linux ifpython
refers to Python 2)
This command creates a new folder named.venv
(or another name if specified) within your project directory, containing a private Python interpreter and package installation location.
- Activate the Environment: After creation, activate the virtual environment:
- Windows:
.\.venv\Scripts\activate
- macOS/Linux:
source ./.venv/bin/activate
Once activated, your terminal prompt will typically change to show(.venv)
or a similar indicator, confirming that you are now working within your project's isolated environment.
- Windows:
- Select the Python Interpreter in VS Code: VS Code will often automatically detect and prompt you to use the newly created virtual environment. If not:
- Open the Command Palette (
Ctrl+Shift+P
orCmd+Shift+P
). - Type
Python: Select Interpreter
and choose the interpreter located within your.venv
folder (e.g.,./.venv/bin/python
).
- Open the Command Palette (
Any Python packages you install using pip
(e.g., pip install requests
) while this environment is active will be installed into .venv
, keeping your project's dependencies neatly managed. You can learn more about this on the official Python Virtual Environments documentation.
Recommended Extensions
To maximize your Python development experience in VS Code, install the official Python extension:
- Python Extension: Developed by Microsoft, this is an essential extension for Python development. It provides rich features such as IntelliSense (code completion), linting, debugging, testing, and much more.
- To install, go to the Extensions view (
Ctrl+Shift+X
orCmd+Shift+X
), search for "Python," and click the "Install" button.
You can find the extension on the Visual Studio Code Python Extension Marketplace page. For a general overview of Python development in VS Code, refer to the VS Code Python documentation.
- To install, go to the Extensions view (
Structuring Your Project (Optional but Recommended)
As your project evolves, adopting a logical directory structure can significantly improve organization and maintainability.
Common project structure examples:
my_python_project/
├── .venv/ # Your virtual environment
├── src/ # Source code for your main application
│ ├── __init__.py
│ ├── main.py
│ └── module1.py
├── tests/ # Unit and integration tests
│ └── test_module1.py
├── data/ # For data files, if applicable
├── requirements.txt # Lists project dependencies (pip freeze > requirements.txt)
├── README.md # Project documentation
└── .gitignore # Files and folders to ignore by Git
Quick Reference Table for Key Actions
Task | Command/Action |
---|---|
Create Virtual Environment (Terminal) | python -m venv .venv |
Activate Virtual Environment (Win) | .\.venv\Scripts\activate |
Activate Virtual Environment (Mac/Linux) | source ./.venv/bin/activate |
Select Python Interpreter (VS Code) | Ctrl+Shift+P -> Python: Select Interpreter |
Install Python Extension | Ctrl+Shift+X -> Search "Python" -> Install |
Add New File (in Explorer) | Click "New File" icon or Right-click in Explorer and choose "New File" |