# Mastering Python Virtual Environments: Setup and Best Practices URL: https://madhudadi.in/blog/posts/python-virtual-environments-setup-and-usage-guide Published: 2026-06-10 Tags: python, setup Read time: 44 min Difficulty: beginner > Learn Python virtual environments with venv: create .venv, activate it, install packages, freeze requirements, recreate environments, and avoid dependency conflicts.# Python Virtual Environments: venv, Activation, pip, and requirements.txt ## Quick Answer A virtual environment is a private Python environment for one project. Use one `.venv` per project. Windows: ```powershell py -m venv .venv .\.venv\Scripts\Activate.ps1 ``` macOS: ```bash python3 -m venv .venv source .venv/bin/activate ``` Last verified: June 9, 2026. ## Official Links - Python `venv` docs: https://docs.python.org/3/library/venv.html - Python tutorial on virtual environments: https://docs.python.org/3/tutorial/venv.html - Python packaging guide: https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/ - pip docs: https://pip.pypa.io/en/stable/ ## What You Will Learn By the end, you should be able to: - explain why virtual environments matter - create a `.venv` folder - activate and deactivate environments on Windows and macOS - install packages inside the environment - create `requirements.txt` - recreate an environment from scratch - know what should and should not go into Git ## 1. Why Virtual Environments Exist Different projects need different packages. Example: ```text project-a needs pandas 2.3 project-b needs pandas 2.0 ``` If both projects use one global Python, packages can conflict. With virtual environments: ```text project-a/.venv has project-a packages project-b/.venv has project-b packages ``` The environment is disposable. Your code is important. `.venv` can always be deleted and recreated. ## 2. Create A Project Folder Windows PowerShell: ```powershell mkdir python-venv-practice cd python-venv-practice ``` macOS: ```bash mkdir python-venv-practice cd python-venv-practice ``` ## 3. Create The Virtual Environment Windows: ```powershell py -m venv .venv ``` macOS: ```bash python3 -m venv .venv ``` Linux: ```bash python3 -m venv .venv ``` `.venv` is the common folder name. Do not put your source code inside `.venv`. ## 4. Activate The Virtual Environment | Platform | Shell | Activation command | | --- | --- | --- | | Windows | PowerShell | `.\.venv\Scripts\Activate.ps1` | | Windows | Command Prompt | `.venv\Scripts\activate.bat` | | macOS | zsh/bash | `source .venv/bin/activate` | | Linux | bash/zsh | `source .venv/bin/activate` | | macOS/Linux | fish | `source .venv/bin/activate.fish` | After activation, your prompt often begins with: ```text (.venv) ``` ## 5. Verify The Environment After activation, run: ```bash python -c "import sys; print(sys.executable)" python -m pip --version ``` Expected result: - the Python path includes `.venv` - pip also points inside `.venv` On Windows, the path may include: ```text .venv\Scripts\python.exe ``` On macOS, the path may include: ```text .venv/bin/python ``` ## 6. Install Packages Inside The Environment After activation: ```bash python -m pip install pandas matplotlib ``` Test: ```bash python -c "import pandas as pd; print(pd.__version__)" ``` The packages are installed into `.venv`, not into your global Python. ## 7. Deactivate To leave the environment: ```bash deactivate ``` This removes `.venv` from the active terminal PATH. ## 8. Create requirements.txt After installing packages: ```bash python -m pip freeze > requirements.txt ``` Example: ```text pandas==2.3.0 matplotlib==3.10.0 ``` Commit `requirements.txt` to Git. ## 9. Recreate The Environment If `.venv` is deleted or someone else downloads your project, recreate it. Windows: ```powershell py -m venv .venv .\.venv\Scripts\Activate.ps1 python -m pip install -r requirements.txt ``` macOS: ```bash python3 -m venv .venv source .venv/bin/activate python -m pip install -r requirements.txt ``` ## 10. What Goes Into Git? Commit: ```text your_code.py requirements.txt README.md data/sample.csv ``` Do not commit: ```text .venv/ __pycache__/ .env ``` Beginner `.gitignore`: ```gitignore .venv/ __pycache__/ *.pyc .env ``` ## 11. PowerShell Activation Error If PowerShell says scripts are disabled, run: ```powershell Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser ``` Then close and reopen PowerShell. Activate again: ```powershell .\.venv\Scripts\Activate.ps1 ``` This changes policy only for your current Windows user. ## 12. macOS PATH Or Activation Problems If activation fails, confirm the folder exists: ```bash ls .venv/bin ``` If `python3 -m venv .venv` fails, check Python: ```bash python3 --version which python3 ``` If the terminal cannot find newly installed tools, restart Terminal or reload zsh config: ```bash source ~/.zshrc ``` ## 13. Should You Use `venv` Or `uv`? Use `venv` when: - you are learning Python fundamentals - a course asks for plain Python tooling - you want to understand environments clearly Use `uv` when: - you want faster project setup - you want lockfiles - you want one tool for Python versions, environments, and packages `uv` is covered in the next post. ## Practice Task Create an environment, install pandas, and run this file: ```python import pandas as pd scores = pd.DataFrame({ "name": ["Asha", "Ben", "Chen"], "score": [88, 91, 79], }) print(scores) print("Average:", scores["score"].mean()) ``` If it runs, your environment works. ## FAQ ### Should I create `.venv` inside every project? Yes. It keeps project dependencies separate and easy to recreate. ### Should I commit `.venv` to Git? No. Commit `requirements.txt` or `pyproject.toml`, then recreate `.venv` later. ### Do I need to activate the environment every time? For terminal work, yes. IDEs can also select the `.venv` interpreter directly. ### Can I move a `.venv` folder to another computer? Do not rely on that. Recreate it from `requirements.txt`. ## Final Checklist Windows: ```powershell py -m venv .venv .\.venv\Scripts\Activate.ps1 python -c "import sys; print(sys.executable)" python -m pip install requests python -m pip freeze > requirements.txt deactivate ``` macOS: ```bash python3 -m venv .venv source .venv/bin/activate python -c "import sys; print(sys.executable)" python -m pip install requests python -m pip freeze > requirements.txt deactivate ``` If the Python path includes `.venv`, the setup is correct.