Every Python ML project should start with a virtual environment. If you’re skipping this, you will eventually break something and spend hours figuring out why.
Here’s what to do and why.
What a virtual environment is
A virtual environment is an isolated Python installation that lives inside your project folder. Packages you install inside it don’t affect your system Python or any other project. Dependencies are contained.
Without one, pip install tensorflow installs TensorFlow globally on your machine. Eventually two projects will want different versions of the same package and you’ll have a conflict that’s genuinely annoying to debug.
Create one
# Create the environment in a folder called .venv
python3 -m venv .venv
That’s it. A .venv folder appears in your project directory.
Activate it
# macOS / Linux
source .venv/bin/activate
# Windows
.venv\Scripts\activate
Your terminal prompt changes to show (.venv) — you’re now inside the environment. Any pip install you run installs only into this environment.
Install packages
pip install torch torchvision opencv-python
These install into .venv, not globally.
Save your dependencies
pip freeze > requirements.txt
This creates a requirements.txt listing every installed package and its version. Anyone else (or future you) can recreate the environment exactly:
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
Add .venv to .gitignore
The environment folder is large and doesn’t belong in version control. Add this to your .gitignore:
.venv/
Commit requirements.txt instead — that’s the portable version.
Deactivate
deactivate
You’re back to system Python.
That’s the whole workflow. 30 seconds per project, saves hours of dependency pain.