How to set up a Python environment on Windows
Overview
Creating and managing isolated environments is crucial for Python projects. You don’t want dependencies clashing between web apps, data‑science scripts, and small automation utilities. On Windows, several tools—built‑in and third‑party—help you maintain clean, reproducible environments. This guide focuses exclusively on environment setup, assuming Python itself is already installed and on your system.
1. PATH & Environment Variables
1.1 Why They Matter
The PATH variable tells Windows where to find executables like python.exe
, pip.exe
, and other scripts. Misconfigured PATH is the top reason “python is not recognized” errors appear.
1.2 Locating Your Python Install
By default, Python lives in one of:
C:\Users\
\AppData\Local\Programs\Python\Python3X\ C:\Program Files\Python3X\
(if installed system‑wide)
1.3 Editing PATH
- Press Win + R, type
sysdm.cpl
, Enter. - Advanced → Environment Variables.
- Under User Variables, edit
Path
. - Add:
C:\Users\
\AppData\Local\Programs\Python\Python3X\ C:\Users\
\AppData\Local\Programs\Python\Python3X\Scripts\ - OK → restart any open terminals.
1.4 Verifying
Open a new Command Prompt or PowerShell and run:
python --version
pip --version
If both print valid versions, PATH is correct.
2. Built‑in venv
Module
The venv
module ships with Python 3.3+. It creates lightweight virtual environments without extra installs.
2.1 Creating an Environment
- Navigate to your project folder:
- Run:
- This makes a hidden
.venv
folder with its ownpython.exe
andScripts
.
cd C:\path\to\project
python -m venv .venv
2.2 Activating & Deactivating
- CMD:
.venv\Scripts\activate.bat
- PowerShell:
.venv\Scripts\Activate.ps1
Prompt shows (.venv)
. To exit:
deactivate
2.3 Managing Packages
Inside an active venv, pip install
installs locally:
pip install requests flask
Freeze and share:
pip freeze > requirements.txt
Reproduce:
pip install -r requirements.txt
3. virtualenv & Pipenv
For more features—faster creation, isolation from system site‑packages—consider virtualenv
, or full dependency management with pipenv
.
3.1 Installing virtualenv
pip install virtualenv
Create a new env:
virtualenv env-advanced
3.2 Installing Pipenv
pip install pipenv
Pipenv creates Pipfile
& Pipfile.lock
:
pipenv install flask requests
Enter shell:
pipenv shell
Exit with exit
.
4. Conda Environments
If you use Anaconda or Miniconda, conda
excels at managing both Python and non‑Python packages.
4.1 Creating
conda create --name myenv python=3.13
4.2 Activating
conda activate myenv
4.3 Installing
conda install numpy pandas
4.4 Exporting & Recreating
conda env export > environment.yml
conda env create -f environment.yml
5. IDE & Editor Integration
5.1 Visual Studio Code
- Install Microsoft’s “Python” extension.
- Open folder; click interpreter selector (bottom‑left).
- Choose
.venv\Scripts\python.exe
or your Conda env. - Integrated terminal inherits the chosen env.
5.2 PyCharm
- Settings → Project → Python Interpreter.
- ⚙️ → Add → Existing environment → point to env’s
python.exe
. - Apply & restart run configurations.
5.3 Jupyter Notebooks
pip install ipykernel
python -m ipykernel install --user --name=myenv --display-name="Python (myenv)"
Select that kernel when you start Jupyter.
6. Windows Subsystem for Linux (WSL)
WSL lets you run Linux distributions that can use Linux‑style Python tools alongside your Windows setup.
6.1 Enabling WSL
wsl --install
Restart when prompted.
6.2 Installing Ubuntu
From Microsoft Store, install “Ubuntu 22.04 LTS,” then launch and create your UNIX user.
6.3 Python in WSL
sudo apt update
sudo apt install python3-venv python3-pip
6.4 Creating & Activating
python3 -m venv ~/venv
source ~/venv/bin/activate
Use code .
in WSL to open folder in VS Code.
7. Troubleshooting
7.1 “python” Not Recognized
Verify PATH includes both the Python and Scripts folders, then restart your terminal.
7.2 Activation Policy Errors
In PowerShell, you may need:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
7.3 Dependency Conflicts
Use pip check
or Conda’s solver to detect mismatches. Pin versions in requirements.txt
or Pipfile.lock
.
8. Best Practices
- One environment per project.
- Commit only lock‑files or freeze‑files, not the env folder itself.
- Use descriptive env names (e.g.
projname-env
). - Regularly update and test dependencies.
- Document activation commands in README.
- Secure sensitive data via environment variables.