A virtual environment is a project-specific Python environment that isolates installed packages from other projects.
Understanding the Concept
In PyCharm, open project interpreter settings and choose to create a new local virtual environment based on Python. A common directory name is .venv. The project then uses that environment’s interpreter. Installing a package in this environment does not normally make it available to every other Python project. Different projects can require different package versions without interfering with one another.
When and Why You Would Use This
Virtual environments are especially useful when a course later introduces third-party tools. For the first examples using the standard library, no extra packages are needed. Keep .venv managed by tooling and do not copy the environment to another computer as if it were portable project source.
Practical Example
python -m venv .venv
# Windows PowerShell: .venv\Scripts\Activate.ps1
# macOS/Linux: source .venv/bin/activate
python --version
Understanding the Example
Run the creation command in a terminal inside your project if you prefer the CLI method. The activation commands are OS-specific and shown as comments for reference. In PyCharm, using the interpreter dialog often performs environment creation without manual activation. The version inside the environment should still be 3.15.
Try It Yourself
Create .venv for a practice project, select it in PyCharm, and verify its Python version.
Common Beginner Mistakes
- Creating a virtual environment from an unintended older interpreter.
- Installing packages globally after creating an environment, then wondering why the project cannot import them.
- Committing the entire .venv directory to a shared source repository.
Key Takeaways
- A virtual environment isolates project-specific dependencies.
- The environment inherits its Python version from the selected base interpreter.
- Use PyCharm’s interpreter settings or python -m venv to create it.