Content · Programming · Python

Python Create a Virtual Environment with python -m venv

python -m venv .venv creates an isolated environment with its own Python executable and normally pip. Use the intended Python executable when starting the environment.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

This guide explains create a virtual environment with python -m venv through a focused Python example. You will learn the behavior to expect, why it matters in a real program, and how to recognize the common mistakes before applying it in your own project.

What You Will Learn


python -m venv .venv creates an isolated environment with its own Python executable and normally pip. Use the intended Python executable when starting the environment.

When and Why to Use It


The following example focuses on a practical use of Create a Virtual Environment with python -m venv.

Step-by-Step Example


# Windows (Python Install Manager with 3.15 installed):
# py -V:3.15 -m venv .venv
# macOS/Linux where python3.15 is installed:
# python3.15 -m venv .venv
# Then verify using the environment's own executable.\n\nfrom pathlib import Path\nprint(Path(".venv") / "pyvenv.cfg")

Understanding the Example


The example shows the commands as comments to distinguish terminal instructions from Python source. The printed path is the configuration file created inside a real environment.

Important Behavior and Edge Cases


An already existing .venv may use a different base interpreter. Recreate it deliberately when switching Python versions.

Try It Yourself


Create a venv, locate pyvenv.cfg and confirm its underlying Python version.

Run environment-management commands in your terminal, not in a Python source file. Use the executable associated with the selected project interpreter and verify the environment before installing dependencies.

Common Beginner Mistakes


  • An already existing .venv may use a different base interpreter. Recreate it deliberately when switching Python versions.
  • Running installation commands against a different Python interpreter than the one used for the project.
  • Skipping the practice step and assuming the example covers every input. Test at least one normal case and one edge case relevant to this topic.

Key Takeaways


  • python -m venv .venv creates an isolated environment with its own Python executable and normally pip.
  • The example shows the commands as comments to distinguish terminal instructions from Python source.
  • Apply the technique deliberately, checking its documented input requirements and failure behavior before integrating it into a larger project.
Share this resourceSend this page to someone who may find it useful.