Your Logo

Creating a Python Project Using a Virtual Environment

A step-by-step tutorial on how to create a Python project with a Virtual Environment

Complete Tutorial: Creating and Testing a Python Project Using 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.


1. Install Python

Make sure Python is installed on your system.


2. Create a Project Directory

Create and navigate to your project folder.


3. Create a Virtual Environment

Create a virtual environment inside your project folder. Use one of the available Python commands depending on your system:

This creates a folder named .venv with your virtual environment.


4. Activate the Virtual Environment

Activate the virtual environment to isolate your project dependencies.

After activation, your prompt typically shows (venv).


5. Install Required Packages

For testing, we will install the requests package inside the virtual environment.

Run:

pip install requests

6. Create a Simple Python Test Script

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()

7. Run the Python Script

Run your script using the command that corresponds to your Python setup, with the virtual environment still activated:

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.


8. Optional: Save Your Dependencies

Save installed packages to a requirements.txt file:

pip freeze > requirements.txt

This file can be used later to recreate the environment.


9. Deactivate the Virtual Environment

When finished, deactivate your environment by running:

deactivate

Summary Table

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