Python 3.14 ·
✓ verified by execution on 2026-07-25
When you start building larger applications, you will often want to use third-party packages (libraries written by others) that do not come with Python’s standard library. To prevent different projects from conflicting with each other, we use virtual environments.
System Python vs Virtual Environment Isolation
Creating a Virtual Environment
A virtual environment is an isolated directory tree that contains a Python installation and additional packages. The venv module is used to create and manage virtual environments. It is part of the standard library, so you can import it directly:
python
import venvprint(venv.__name__)
Output
venv
Your output
A common directory location for a virtual environment is .venv. You can create one by running this command in your terminal:
$ python -m venv .venv
To use the environment, you must activate it. This modifies your shell’s path so that python points to the isolated installation.
$ source .venv/bin/activate
(On Windows, you would run .venv\Scripts\activate instead).
Checking if a Virtual Environment is Active
You can programmatically verify whether your script is running inside a virtual environment. When activated, Python changes sys.prefix to point to the virtual environment directory, while sys.base_prefix continues to point to the system Python installation.
import sys# We check if we are running inside a virtual environment# by comparing sys.prefix with sys.base_prefixin_venv = sys.prefix != getattr(sys, 'base_prefix', sys.prefix)print(in_venv)
False
(Since you are running this code in the CodeLudo sandbox, no virtual environment is active!)
Installing Packages with pip
Once activated, you use pip to manage packages. pip installs packages from the Python Package Index (PyPI). You can verify pip is available:
For example, to install the popular requests library:
$ python -m pip install requests
You can view detailed information about an installed package:
$ python -m pip show requests
Freezing Requirements
To share your project with others, they need to know which packages to install. pip freeze produces a list of installed packages in the format that pip install expects.
$ python -m pip freeze > requirements.txt
Another developer can then clone your project, create their own virtual environment, and install all dependencies in one go:
$ python -m pip install -r requirements.txt
Check yourself
What does the pip command do?
Reveal answer
Installs packages from the Python Package Index. — The pip command automatically downloads and installs packages from the Python Package Index (PyPI).
If you install a package with pip, is it automatically available to every Python project on your computer?
Reveal answer
No, if installed inside an active virtual environment, packages are only available when that specific environment is active. — Virtual environments keep dependencies isolated. Packages installed in an active virtual environment are not shared with the rest of the system.
Which command saves your project's current dependencies into a file?
Reveal answer
pip freeze > requirements.txt — pip freeze produces a list of installed packages in the format that pip install expects. You typically redirect this output to requirements.txt.
Challenges
Challenge 1 +15 XP
You are given a string `package_line = "requests==2.26.0"` from a requirements.txt file. Write code to extract just the package name and assign it to the variable `package_name`.
python
Test 1 — expects ""
Need a hint? (−25% XP)
Use the `.split()` method on the string with '==' as the separator, and get the first element of the resulting list.
You have a list of required packages: `['requests==2.26.0', 'numpy==1.21.2']`. Write a loop that opens a file named `requirements.txt` in write mode and writes each package on a new line.
python
Test 1 — expects ""
Need a hint? (−25% XP)
Use a `with open(...)` block, loop through the list, and write each item plus a newline character `\n`.
Show solution (0 XP)
packages = ['requests==2.26.0', 'numpy==1.21.2']with open('requirements.txt', 'w') as f: for pkg in packages: f.write(pkg + '\n')