Fundamentals

Create a Python Virtual Environment for AI

This guide shows you how to create, activate, and manage a Python virtual environment for an AI project in about ten minutes, on Mac, Windows, or Linux. A virtual environment is a private folder that holds its own copy of Python and its own installed packages, kept completely separate from the Python that came with your computer. It is the single habit that prevents the most common beginner headache: two projects fighting over which version of a package is installed.

Here is the problem it solves. Say you install the openai SDK (the official toolkit for calling OpenAI models) straight into your system Python for a blog-writing script. Months later you start a second project that needs a newer, slightly incompatible release. Upgrade it for the new project and the old one breaks; keep the old one and the new project will not run. Only one version can exist at a time. A virtual environment per project removes that conflict entirely, because each folder carries its own pinned packages and they never see each other. This page lives inside Setting Up Python for AI, part of the Python AI Fundamentals for Non-Developers track.

Prerequisites

You only need Python 3.10 or newer installed and reachable from your terminal (the text window where you type commands instead of clicking). If you have not done that yet, follow the platform guide that matches your machine: How to Install Python for AI Projects on Mac or How to Install Python for AI on Windows. Confirm the version before you start:

python3 --version

On Windows the command is usually python --version. Either way you want to see Python 3.10 or higher. The venv tool used below ships inside Python itself, so there is nothing extra to install.

1. Create the virtual environment

Make a folder for your project, move into it, and ask Python to build an environment named .venv inside it. The leading dot keeps the folder tidy and out of the way, and .venv is the name editors like VS Code look for automatically.

On Mac or Linux:

mkdir ai-project && cd ai-project
python3 -m venv .venv

On Windows (PowerShell):

mkdir ai-project; cd ai-project
python -m venv .venv

This creates a .venv subfolder containing a private Python interpreter and a place for packages. It takes a few seconds and produces no output when it succeeds. You have not changed anything yet — the environment exists but is not active. Think of this step as building a clean room; the next step is walking into it.

A word on naming and placement. People sometimes call the folder env or venv instead of .venv, and that works, but the dotted name is worth adopting as a habit for two reasons: editors and tools look for .venv by default, and the leading dot hides the folder from casual file listings so it stops cluttering your view. Keep exactly one environment per project, sitting at the top level of the project folder, right next to your code and your requirements.txt. Do not nest a project inside another project's environment, and do not share one environment across several projects — that reintroduces the version conflict you are trying to avoid. If you ever want a completely fresh start, you can safely delete the whole .venv folder and run the create command again; nothing in it is precious, because requirements.txt is the real record of what your project needs.

2. Activate the environment

Activating tells your terminal to use the environment's private Python and pip instead of the system ones. The command differs by platform, which is the single most common point of confusion, so use the line that matches yours exactly.

On Mac or Linux:

source .venv/bin/activate

On Windows (PowerShell):

.venv\Scripts\Activate.ps1

On Windows (older Command Prompt, cmd.exe):

.venv\Scripts\activate.bat

You will know it worked because your prompt now starts with (.venv). From this point, every pip install lands inside the environment, and python runs the isolated interpreter. When you are done working, type deactivate to step back out — the folder stays on disk, ready to activate again next time. You activate once per terminal session; closing the terminal deactivates it automatically.

If activation feels like a step you will forget, that is normal at first, and the (.venv) marker in your prompt is your safety check. Before you install anything or run a script, glance at the start of the line: no (.venv), no isolation. Many editors smooth this over for you. In VS Code, once you select the interpreter inside .venv, any terminal it opens activates the environment automatically, and so does the green Run button. Until that becomes second nature, the habit to build is simple — open a terminal, move into the project folder, activate, then work.

3. Install your AI packages

With (.venv) showing, upgrade pip (Python's package installer) so you avoid stale-version warnings, then install the core stack for API-based AI work. We use the openai SDK for model calls, httpx for any direct HTTP requests (it handles modern features requests does not), and python-dotenv to load secrets from a file.

pip install --upgrade pip
pip install openai httpx python-dotenv

These packages install only inside .venv, so your system Python stays untouched. To prove the isolation is real, ask your shell which Python is active — the path should point inside your project's .venv folder:

# Mac/Linux
which python
# Windows PowerShell
where.exe python

If that path runs through your .venv folder, every install from now on is safely contained.

It is worth understanding what just happened, because it explains why the isolation holds. pip installs packages into the Python it is bound to, and activating the environment swapped in the pip that lives inside .venv. So the three commands you ran — upgrade pip, install the packages, check the path — all operated on the private copy, never on your system Python. That is the whole mechanism: one folder, one interpreter, one set of packages, with nothing leaking out to other projects or in from them.

4. Freeze requirements.txt

A requirements.txt file is a plain list of the exact package versions your project uses. It lets you — or a teammate, or your future self on a new laptop — rebuild an identical environment with one command. Generate it from the currently installed packages:

pip freeze > requirements.txt

Open the file and you will see pinned lines like openai==1.51.0. To recreate the environment elsewhere, someone clones your project, creates and activates a fresh .venv, and runs:

pip install -r requirements.txt

Re-run pip freeze > requirements.txt whenever you add or upgrade a package, and commit the updated file. This is what makes an AI project reproducible instead of "works on my machine."

One subtle point worth knowing: pip freeze records every installed package, including the smaller libraries your main packages depend on. That is fine and even helpful for exact reproduction. If you later want a cleaner file that lists only the packages you chose directly — openai, httpx, python-dotenv — you can write those by hand into requirements.txt instead, and pip install -r requirements.txt will pull in their dependencies automatically. For a beginner, the frozen version is the safer choice, because it pins everything and removes any chance of a surprise upgrade changing behaviour between machines.

5. Ignore the environment in Git

The .venv folder can hold thousands of files and is specific to your operating system, so it should never go into version control. Neither should any .env file holding API keys. Create or append to .gitignore in your project root:

echo ".venv/" >> .gitignore
echo ".env" >> .gitignore

That is the rule to internalize: commit requirements.txt, ignore .venv and .env. Whenever you store a credential in a .env file, add .env to .gitignore in the same breath so a key never lands in a public repository.

Quick reference

CommandDefault / whereEffect
python -m venv .venvcreates .venv/ in current folderbuilds the isolated environment
source .venv/bin/activateMac/Linux onlyactivates the environment for this session
.venv\Scripts\Activate.ps1Windows PowerShell onlyactivates the environment for this session
pip freeze > requirements.txtwrites to project rootrecords exact installed versions
deactivateworks on any platformexits the environment, returns to system Python

Troubleshooting

  1. Activate.ps1 cannot be loaded because running scripts is disabled — Windows PowerShell blocks scripts by default. The cause is the execution policy. Run Set-ExecutionPolicy -Scope CurrentUser RemoteSigned once, answer Y, then activate again. This affects only your user account, not the whole system.
  2. pip install puts packages somewhere unexpected, or imports fail — Your environment is not active. The cause is a fresh terminal, which always starts deactivated. Re-run the activation command for your platform and confirm (.venv) appears in the prompt before installing.
  3. which python points to the wrong interpreter — Your editor or terminal is still using system Python. The cause is usually a tool that opened before you activated. Close and reopen the terminal, activate again, and in VS Code use "Python: Select Interpreter" to choose the one inside .venv.
  4. source: command not found on Windows — The source command is Mac and Linux only. The cause is copying a Mac instruction into PowerShell. Use .venv\Scripts\Activate.ps1 instead, with backslashes.
  5. No module named openai even though you installed it — You installed into one environment and are running from another, or from system Python. The cause is almost always a missing or mismatched activation. Activate the project's .venv, run pip show openai to confirm the package is present, then run your script from that same active session.

When to use this vs. alternatives

  • Use venv for almost every project in this track. It ships with Python, needs nothing extra, and is the right default for API-based AI work that mostly installs the openai SDK and a few helpers. If you are unsure, choose venv.
  • Use conda when a project depends on heavy scientific or GPU libraries with non-Python parts — think CUDA-backed deep-learning stacks. conda manages those system-level dependencies that plain pip cannot, at the cost of a larger install and a slower workflow.
  • Use uv when speed matters and you are comfortable trying a newer tool. uv creates environments and installs packages in seconds rather than minutes, with commands close to venv (uv venv then uv pip install ...). It is an excellent upgrade once the basics here feel natural, and it reads the same requirements.txt you already produce.

With an isolated environment in place, you are ready to install AI packages confidently and run your first model call. A good next step is to compare which models to call in OpenAI vs Anthropic API for Beginners or to find no-cost options in Best Free AI APIs for Beginners. Back to Setting Up Python for AI.