A step-by-step tutorial on how to create a Python project with a Virtual Environment
This tutorial guides you step-by-step through creating a Python project with a virtual environment and testing it
with a simple Python script. Commands are provided for both Linux/macOS and Windows. It also accounts for
different Python command usages: python, python3, and py. Examples of all
3 options will be provided. Be sure to use the option that is applicable to your system configuration.
NOTE: This tutorial assumes that you have a basic understanding of your operating system (Windows, macOS, or Linux), familiarity with Python programming fundamentals, and experience using the command line/terminal. If you are new to any of these topics, you may want to review introductory materials before proceeding.
Make sure Python is installed on your system.
Check by running one of these commands:
Linux/macOS options, bash:
python --version
python3 --version
Windows (Command Prompt or PowerShell):
python --version
python3 --version
py --version
If Python is not installed, download it from python.org and install it.
Create and navigate to your project folder.
Linux/macOS:
mkdir my_python_project
cd my_python_project
Windows (CMD / PowerShell):
mkdir my_python_project
cd my_python_project
Create a virtual environment inside your project folder. Use one of the available Python commands depending on your system:
Linux/macOS options, bash:
python -m venv .venv
python3 -m venv .venv
Windows (CMD / PowerShell) options:
python -m venv .venv
python3 -m venv .venv
py -m venv .venv
This creates a folder named .venv with your virtual environment.
Activate the virtual environment to isolate your project dependencies.
Linux/macOS options, bash:
source .venv/bin/activate
Windows Command Prompt (CMD):
.venv\Scripts\activate.bat
Windows PowerShell:
.venv\Scripts\Activate.ps1
After activation, your prompt typically shows (venv).
For testing, we will install the requests package inside the virtual environment.
Run:
pip install requests
Create a new file named app.py in your project directory and add the following code:
# app.py
import requests
def main():
print("Virtual environment test: Starting script...")
try:
url = "https://www.google.com"
response = requests.get(url)
print(f"Fetched {url} with status code: {response.status_code}")
except Exception as e:
print(f"Error fetching website: {e}")
print("Virtual environment test: Script completed successfully.")
if __name__ == "__main__":
main()
Run your script using the command that corresponds to your Python setup, with the virtual environment still activated:
Linux/macOS options, bash:
python app.py
python3 app.py
Windows (CMD / PowerShell) options:
python app.py
python3 app.py
py app.py
You should see output similar to:
Virtual environment test: Starting script...
Fetched https://www.google.com with status code: 200
Virtual environment test: Script completed successfully.
This confirms your virtual environment and Python setup are working correctly.
Save installed packages to a requirements.txt file:
pip freeze > requirements.txt
This file can be used later to recreate the environment.
When finished, deactivate your environment by running:
deactivate
| Step | Linux/macOS Command | Windows Command (CMD / PowerShell) |
|---|---|---|
| Create directory & cd | mkdir my_python_project && cd my_python_project |
mkdir my_python_project cd my_python_project |
| Create virtualenv | python -m venv .venv or python3 -m venv .venv |
python -m venv .venv or python3 -m venv .venv or
py -m venv .venv |
| Activate venv | source venv/bin/activate |
venv\Scripts\activate.bat (CMD) venv\Scripts\Activate.ps1
(PowerShell) |
| Install packages | pip install requests |
pip install requests |
| Run script | python app.py or python3 app.py |
python app.py or python3 app.py or py app.py |
| Save dependencies | pip freeze > requirements.txt |
pip freeze > requirements.txt |
| Deactivate virtualenv | deactivate |
deactivate |