Skip to main content
In this lesson we’ll set up the tools and configure a Python virtual environment to isolate project dependencies and streamline development. For details on the built-in venv module see the official docs: https://docs.python.org/3/library/venv.html.
A presentation slide reading "Configuring our Virtual Environment" with a large blue "Demo" label on a dark curved shape to the right. A small "© Copyright KodeKloud" appears in the bottom-left.
Why use a virtual environment?
  • Isolates a project’s dependencies from the system Python and from other projects.
  • Lets different projects use different package or Python versions (for example, one project can use OpenCV 4.x while another uses 3.x). See OpenCV: https://opencv.org/
  • Makes projects portable: export dependencies with a requirements file and others can recreate the environment exactly using pip install -r requirements.txt. See pip requirements docs: https://pip.pypa.io/en/stable/topics/requirements-files/
Editor tooling (example: VS Code)
  • Use editor extensions to speed development and reduce boilerplate. Popular choices include GitHub Copilot, GitHub Copilot Chat, Blackbox, and Tabnine.
  • When you enable GitHub Copilot you will authenticate through GitHub.
  • You can toggle auto-complete and other extension features depending on how much in-editor assistance you want.
Sample Flask routes (example code you might develop inside your virtual environment)
Creating a Python virtual environment Open a terminal in your project directory (for example, imageoptimizer) and run:
Notes on naming
  • The venv folder can be named anything (image-optimizer, venv, myenv). Many teams use venv for consistency and to simplify .gitignore rules.
  • The command above creates a venv directory containing an isolated Python interpreter, pip, and site-packages for that environment.
Activate the virtual environment Use the activation command appropriate for your shell and OS. The table below lists common commands: After activation your prompt typically shows the environment name:
When activated, the python and pip commands refer to the virtual environment’s binaries; you no longer need to run python3 explicitly. What the venv folder contains (overview) Inside the created venv directory you will commonly find:
  • bin/ (macOS/Linux) or Scripts\ (Windows) — executables and activation scripts
  • include/ — C headers used when compiling extension modules
  • lib/ or Lib\ — installed Python packages
  • pyvenv.cfg — configuration for the virtual environment
Example snippet from an activate script for csh (truncated and illustrative)
Working with dependencies
  • Install packages inside the activated venv:
  • Freeze the current environment to a requirements.txt for reproducibility:
  • Recreate the environment elsewhere:
Add your virtual environment directory (for example, venv/) to .gitignore so installed packages are not committed to source control.
Do not commit your virtual environment directory to Git. Instead commit requirements.txt (or a lock file) so collaborators can recreate the environment reliably.
Summary
  • A Python virtual environment isolates dependencies per project so multiple projects can use different package or Python versions.
  • Create a venv with python3 -m venv venv, activate it with the proper activation script for your shell/OS, then install packages using pip.
  • Export dependencies with pip freeze > requirements.txt and recreate environments with pip install -r requirements.txt.
  • Editor extensions (e.g., GitHub Copilot, Tabnine) can speed development but do not change how virtual environments work.
Links and references

Watch Video