Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Python provides the built-in venv module in its standard library to create lightweight, isolated virtual environments. In this guide, we cover the exact command-line steps to generate, activate, verify, and deactivate virtual environments across Windows, macOS, and Linux.
Open your terminal or command prompt, navigate to your project's root folder, and execute the venv module:
(On some Linux/macOS systems, you may type python3 -m venv .venv).
.venv?. makes the directory hidden on UNIX systems..venv and prompt you to select it as your project's active interpreter..venv/.Activation modifies your active shell session's PATH environment variable so that typing python or pip runs the executables inside your .venv directory instead of global system executables.
The activation script depends on your operating system and shell:
Execution_Policies restriction, allow scripts for the current terminal session:
```powershell
Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned
.venv\Scripts\Activate.ps1
```When successfully activated:
.venv directory:where python $\rightarrow$ ...\Project\.venv\Scripts\python.exewhich python $\rightarrow$ .../Project/.venv/bin/pythonWhen you finish working on your project, return your shell back to the global system environment by simply typing:
The (.venv) prefix disappears immediately from your prompt.
.gitignore.venv/) to Git, GitHub, or any version control system!
Virtual environments contain thousands of platform-specific binary executables and C-extensions compiled specifically for your exact operating system and CPU architecture.Always add .venv to your project's .gitignore file:
Instead of committing the environment itself, you commit the dependency specification (requirements.txt), allowing collaborators to recreate the environment on their own machines with a single command.
When running venv\Scripts\Activate.ps1 on Windows, PowerShell may block the script. Resolve this by opening PowerShell and running: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser.
If you run pip install without seeing (.venv) in your terminal prompt, packages will be installed to your global environment instead of your virtual environment.
Create a new isolated virtual environment named .venv in your project folder, activate it, verify which Python interpreter is active, and deactivate it.
.venv in macOS/Linux terminal?.venv/) NEVER be committed to Git?In the next lesson, we will continue your intermediate Python journey with Using pip for Package Management (12: Virtual Environments & Pip).
👉 Continue to Next Lesson: Using pip for Package Management →
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Why Use Virtual Environments | Using pip for Package Management |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.