Set Up a Python Project with Virtual Environment
Create an isolated Python environment with dependencies, linting, and a reproducible project structure ready for collaboration.
Prerequisites
- -Python 3.8+ installed
Steps
Create the project directory
Create a new directory for your project and navigate into it.
Use a descriptive name that matches your package or application name.
Create a virtual environment
Creates an isolated Python environment in the .venv directory. This keeps project dependencies separate from your system Python.
The .venv name is a widely adopted convention. Most editors and tools auto-detect it.
Activate the environment
Activates the virtual environment so pip and python commands use the isolated copy. Your shell prompt will change to indicate the active environment.
You must activate the environment in every new terminal session. Forgetting to activate is the most common cause of 'module not found' errors.
Upgrade pip and install dependencies
Upgrade pip to the latest version and install your project dependencies. Always upgrade pip first to avoid compatibility warnings.
Freeze dependencies to requirements.txt
Lock all installed packages and their exact versions into a requirements file. This ensures reproducible installs across machines and CI.
Run this command every time you add or update a dependency to keep the file current.
Add .venv to .gitignore
Exclude the virtual environment directory from version control. Each developer should create their own .venv from requirements.txt.
Never commit the .venv directory. It contains platform-specific binaries that will not work on other machines.
Full Script
FAQ
Discussion
Loading comments...