Project setup: virtual environments, requirements, .env files and Git
Python for AI Apps · 10 questions
Short notes
Key idea
Every project gets its own virtual environment, a pinned list of dependencies, secrets in a .env file that never reaches Git, and version control from the first day.
Syntax
python -m venv .venv
.venv\Scripts\activate # Windows; source .venv/bin/activate on Mac/Linux
pip install google-genai pydantic python-dotenv
pip freeze > requirements.txt
git init
.gitignore:
.venv/
.env
__pycache__/
from dotenv import load_dotenv
load_dotenv() # reads .env into environment variables
Example
A teammate clones the repo, creates a venv, runs pip install -r requirements.txt, copies .env.example to .env and fills in their own key. The app runs the same on both machines.
Traps
- Installing into the global Python mixes projects' versions and breaks one when you update another.
- Committing
.envonce leaks the key forever in Git history; rotate it. - Unpinned packages change underneath you; pin versions.
- Commit
.env.examplewith dummy values so others know what's needed.
Want the full lesson with step-by-step examples, a project and a certificate? Python for AI Apps course, ₹180 →
- Question 1easy
Why create a virtual environment for each project?
- Question 2easy
What does
pip freeze > requirements.txtdo? - Question 3medium
What does this print?
import os os.environ["MAX_OUTPUT_TOKENS"] = "400" value = os.environ.get("MAX_OUTPUT_TOKENS", "300") print(value + "0", int(value) + 100) - Question 4easy
Which file should be in .gitignore?
- Question 5medium
You accidentally pushed your API key to GitHub and then deleted the file in a new commit. Is that enough?
Project setup: virtual environments, requirements, .env files and Git
Project setup: virtual environments, requirements, .env files and Git
Project setup: virtual environments, requirements, .env files and Git
Project setup: virtual environments, requirements, .env files and Git
Project setup: virtual environments, requirements, .env files and Git
5 more questions on this topic, with hints, and tests
Sign up free for 21 days of every question and test.