The venv module creates isolated Python environments for project dependencies. Activate it to use project-specific packages without affecting system-wide installations. Activation differs across operating systems and shells.
| Operating System | Shell Type | Activation Command |
|---|---|---|
| Linux/macOS | Bash/Zsh | source venv/bin/activate |
| Linux/macOS | Fish | source venv/bin/activate.fish |
| Linux/macOS | C Shell | source venv/bin/activate.csh |
| Windows | Command Prompt | venv\Scripts\activate.bat |
| Windows | PowerShell | venv\Scripts\Activate.ps1 |
| Windows | Git Bash | source venv/Scripts/activate |
1. Create a Virtual Environment
Create a virtual environment before activation. Navigate to your project directory and run:
$ python3 -m venv venv
This creates a directory named venv containing the Python interpreter and dependency packages.
Create the environment at a specific location with an absolute path:
$ python3 -m venv /home/user/projects/myapp/venv
2. How To Activate venv on Linux and macOS
Source the activation script from the bin directory:
$ source venv/bin/activate
The shell prompt changes to show the environment name:
(venv) $
This indicates the virtual environment is active. All Python commands now use the isolated environment.
Alternative Shells on Linux/macOS
Fish shell users run:
$ source venv/bin/activate.fish
C shell users run:
$ source venv/bin/activate.csh
3. How To Activate venv on Windows
Activation on Windows depends on the shell. Commands differ between Command Prompt, PowerShell, and Git Bash.
Command Prompt
Run the batch file from the Scripts directory:
C:\> venv\Scripts\activate.bat
PowerShell
Execute the PowerShell script:
PS C:\> venv\Scripts\Activate.ps1
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser to allow local scripts.
Git Bash on Windows
Use forward slashes with the source command:
$ source venv/Scripts/activate
4. Verify Active Environment
Check which Python interpreter is in use:
$ which python
Output shows the path to the virtual environment:
/home/user/project/venv/bin/python
On Windows with Command Prompt:
C:\> where python
View the environment path variable:
$ echo $VIRTUAL_ENV
5. Install Packages in Active Environment
Install packages with pip after activation:
$ pip install requests flask pandas
Packages install only in the active environment. Other projects remain unaffected.
List installed packages:
$ pip list
Generate a requirements file:
$ pip freeze > requirements.txt
6. Deactivate the Virtual Environment
Return to the system Python installation:
$ deactivate
The environment name disappears from the prompt. The system Python becomes active again.
Common Activation Issues
Verify you are in the correct directory. The activation script path must match your environment location.
Check file permissions on Unix systems:
$ chmod +x venv/bin/activate
Windows users may need to adjust execution policies for PowerShell scripts. Administrator privileges are required for system-wide policy changes.
Git Bash users must use forward slashes in paths, even on Windows.
FAQs
Run source venv/bin/activate on Linux/macOS or venv\Scripts\activate.bat on Windows Command Prompt. The command varies by operating system and shell type.
Virtual environments isolate project dependencies. Each project maintains separate package versions without conflicts. This prevents one project’s requirements from breaking another project.
Specify the full or relative path to the activation script. You can activate from any location by providing the correct path to your virtual environment directory.
The shell PATH variable changes. Python and pip commands use the virtual environment’s interpreter and packages. Installed packages affect only this environment, not system-wide installations.
Run the deactivate command. This works on all operating systems. The environment name disappears from your prompt, and system Python becomes active again.
Conclusion
Activating a virtual environment isolates Python packages per project. The activation command depends on your operating system and shell. Linux and macOS use the source command, while Windows requires different scripts for Command Prompt and PowerShell.
Create separate environments for each project. This prevents dependency conflicts and ensures reproducible builds. Deactivate when finished to return to system defaults.