Skip to content

Python Virtual Environment Primer

Cristel Chandre edited this page Nov 29, 2025 · 2 revisions

Python Virtual Environment Primer (Windows, macOS, Linux)

This guide helps you create and activate an isolated Python environment using venv on Windows, macOS, and Linux.


Prerequisites

  • Python 3.x installed (preferably 3.6+)
  • venv module is included by default with Python 3.3+
  • Verify your installation:
    python --version     # or python3 --version

1. Create a Virtual Environment

To create a virtual environment (named pypolar_env, though you can choose any name), open a terminal or command prompt in your project directory and run the following command:

πŸ’» Windows

python -m venv pypolar_env

🍎 macOS / 🐧 Linux

python3 -m venv pypolar_env

This creates a subdirectory called pypolar_env/ that contains the isolated Python interpreter and dependencies.


2. Activate the Virtual Environment

πŸ’» Windows (Command Prompt)

pypolar_env\Scripts\activate

πŸ’» Windows (PowerShell)

pypolar_env\Scripts\Activate.ps1

Note: If you get an execution policy error in PowerShell, run:

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

🍎 macOS / 🐧 Linux

source pypolar_env/bin/activate

When activated, your shell prompt will prefix with (pypolar_env).


3. Use the Environment

Once the environment is active:

  • Install packages with pip, e.g., Polarimetry:

    pip install polarimetry
  • These packages will be isolated to the virtual environment.


4. Deactivate the Environment

When you're done working in the virtual environment, deactivate it:

deactivate

5. Optional Tips

Check installed packages:

pip list

Save your environment:

pip freeze > requirements.txt

Recreate environment from file:

pip install -r requirements.txt

Additional Notes

  • You can safely delete the pypolar_env/ folder to remove the environment.
  • IDEs like VS Code automatically detect and use the pypolar_env if placed in your project folder.
  • For project-based work, it is good practice to include the pypolar_env/ folder in your .gitignore.

Clone this wiki locally