Python Virtual Environments & pip

Manage project dependencies.

✍️ 3 exercises + code challenge in this lesson

Real projects depend on third-party packages. pip installs them from the Python Package Index (PyPI).

Virtual environments

A virtual environment keeps each project's packages separate, so projects don't conflict:

  1. Create: python -m venv .venv
  2. Activate on Windows: .venv\Scripts\activate; on macOS/Linux: source .venv/bin/activate
  3. Install: pip install requests
  4. Save the list: pip freeze > requirements.txt
  5. Reinstall elsewhere: pip install -r requirements.txt
In the browser editor, pure-Python packages can be installed with micropip, as the example shows.

Key Takeaways: Python Virtual Environments & pip

Mastering Python Virtual Environments & pip helps you write clean and reliable Python code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Python Virtual Environments & pip Code Example

Python
import micropip
await micropip.install("emoji")

import emoji
print(emoji.emojize("Python is :fire: and :snake:!"))
Free sign-in required · Google login available

Python Virtual Environments & pip Practice Exercises

Check what you learned in “Python Virtual Environments & pip”.

/ 3 solved
Fill in the blank

Create a virtual environment named .venv.

python -m  .venv
✓ Correct! Not quite. Try again.
Multiple choice

Which command saves the installed packages to a file?

✓ Correct! Not quite. Try again.
Code challenge

Import the built-in statistics module and print the mean of [2, 4, 6].

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services