How to Set Up a Virtual Environment in Windows 11
Note: This guide is tailored for Windows 11 users. Steps may vary slightly for other operating systems.
Step-by-step Tutorial
- Open the Command Prompt or PowerShell:
- Press Win + X and select "Windows Terminal" or "Command Prompt"
- Verify Python installation:
- Type
python --version
and press Enter
- Ensure you have Python 3.3 or newer installed
- Navigate to your project directory:
- Use the
cd
command, e.g., cd C:\Users\YourUsername\Projects\MyProject
- Create a virtual environment:
- Type
python -m venv myenv
(replace "myenv" with your preferred name)
- This creates a new directory named "myenv" in your current location
- Activate the virtual environment:
- Type
myenv\Scripts\activate
- Your prompt should change to indicate the active environment
- Verify the virtual environment:
- Type
where python
- The path should point to your virtual environment's Python executable
- Install packages (optional):
- Use
pip install packagename
to install required packages
- Create a requirements.txt file (recommended):
- Type
pip freeze > requirements.txt
to save your project dependencies
- Deactivate the virtual environment when done:
- Simply type
deactivate
and press Enter
Tip: To recreate this environment on another machine or after deleting it, navigate to the project directory and run:
python -m venv myenv
followed by pip install -r requirements.txt