Python for AIPython 3.10+ · Pydantic 2.12
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
34 small wins to finish your pathNext lesson →

Virtual environments and pip

A virtual environment is a folder with its own Python and its own packages for one project, and pip is the tool that installs packages into it.

Last updated: 30 Sep, 2026 · Python 3.14

Everything up to Timeouts and retries ran in the editor beside each lesson. From here the lessons run in a terminal on your computer, in an empty folder made for the project. The commands are shown for macOS and Linux, with a note where Windows differs.

python -m venv, activate and pip install · from Different Ways Of Creating Python Environment In Data Science Projects- Conda,Virtualenv,Python · 1:23 to 3:44
The video runs Windows cmd: python -m venv myenv, then myenv\Scripts\activate, then pip install numpy. On macOS and Linux the activate line is source myenv/bin/activate, and in PowerShell myenv\Scripts\Activate.ps1. Its Python is 3.8.5, which no longer gets updates; use Python 3.10 or later from python.org. The page names the folder .venv and installs pydantic instead of numpy.

Syntax:

bash
python3 -m venv .venv            # create
source .venv/bin/activate        # switch this terminal to it
pip install package==version     # install into it

Creating a virtual environment

Example
python3 -m venv .venv
source .venv/bin/activate
which python

python3 -m venv .venv creates a folder called .venv holding its own copy of Python and its own place for packages. source .venv/bin/activate switches this terminal to it, and which python shows that python now means the one inside .venv. On your computer the path starts with your project folder; it ends in .venv/bin/python either way.

On Windows
Use python -m venv .venv, then .venv\Scripts\activate. Most terminals show (.venv) at the start of the prompt while it is active.

Installing a package with pip

Example
python3 -m venv .venv
source .venv/bin/activate
pip install --quiet pydantic==2.12.5
python -c "import pydantic; print(pydantic.VERSION)"

pip install downloads a package and the packages it needs. ==2.12.5 pins an exact version, and --quiet hides the download progress. python -c runs one line of Python, here to prove the import works. If pip also prints a notice that a newer pip is available, it is harmless.

Writing down what is installed

Example
python3 -m venv .venv
source .venv/bin/activate
pip install --quiet pydantic==2.12.5
pip freeze > requirements.txt
cat requirements.txt

pip freeze lists every installed package with its exact version, and > writes that list into requirements.txt instead of the screen. Pydantic brought its own dependencies with it.

Commit requirements.txt with your code and leave .venv out. Anyone setting the project up runs pip install -r requirements.txt inside their own environment and gets the same versions.

One Python for everything vs an environment per project

Everything installed globallyA .venv per project
Two projects need different versionsOne of them breaksBoth work
Removing a project's packagesUninstall one by oneDelete the .venv folder
Sharing the setupHard to say what was installedrequirements.txt

Where virtual environments show up in AI work

  • Every framework course after this one starts with a pip install into a fresh environment.
  • AI packages move fast; pinning versions in requirements.txt keeps a working project working.
Watch out. A new terminal starts without the environment. If import pydantic suddenly fails, run source .venv/bin/activate again before reinstalling anything.
Try it yourself
  • Run deactivate, then which python3 again. (which python may say not found on macOS outside an environment; that is expected.)
  • Install pytest as well; pytest needs it.
  • Run pip list and compare it with pip freeze.

Every expert started right here.