To create a simple Python project with dependencies, you need to set up a virtual environment, define your dependencies in a requirements.txt file, and write your code in main.py. This approach ensures project dependencies are isolated and easily installable on any system.
Project Setup Steps
1. Create a Project Directory
Open your terminal or command prompt and create a new directory for your project, then navigate into it:
mkdir simple-python-project
cd simple-python-project
2. Create and Activate a Virtual Environment
A virtual environment keeps your project’s dependencies separate from other Python projects.
Create Virtual Environment:
python -m venv venv
Activate the environment:
# macOS/Linux
source venv/bin/activate
# Windows
.\venv\Scripts\activate
You should see (venv) at the beginning of your terminal prompt, indicating the environment is active.
(venv) D:\python\simple-python-project>
3. Create the requirements.txt file
This file lists the external libraries your project needs. For this example, we will use the requests library to make an HTTP request and python-dotenv to load configuration from .env i.e. the environment file.
Create a file named requirements.txt in your project root and add the following line:
requests
python-dotenv
4. Install Dependencies
With your virtual environment activated, install the packages listed in requirements.txt:
(venv) D:\python\simple-python-project> pip install -r requirements.txt
5. Create main.py
Create the main application file named main.py and add some Python code that uses the installed dependency:
import os
import requests
import sys
from dotenv import load_dotenv
# 1. Load variables from the .env file
load_dotenv()
def main():
# 2. Get the API URL from environment variables
api_url = os.getenv("API_URL")
print(f"Attempting to fetch data from: {api_url}")
if not api_url:
print("Error: API_URL not found in .env file.")
return
try:
response = requests.get(api_url)
response.raise_for_status()
data = response.json()
# GitHub events return a list; we check if it's not empty
if isinstance(data, list) and len(data) > 0:
print(f"Successfully fetched {len(data)} events.")
# Note: Not all events have an 'action' key, using .get() for safety
action = data[0].get('payload', {}).get('action', 'N/A')
print(f"First event payload snippet (action): {action}")
else:
print("Fetched data, but it wasn't in the expected list format.")
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
sys.exit(1)
if __name__ == "__main__":
main()
This script safely reach out to the internet, grab specific data, and show it to you.
6. Create Environment Variables
Create a .env file. This file is used to store secrets like API keys, API endpoints etc. We’ll add the API URL here to show how to load external config.
API_URL=https://api.github.com/events
7. Run the Project
(venv) D:\python\simple-python-project> python main.py
You will see output confirming that the requests library successfully made the web request.
# Output
Attempting to fetch data from: https://api.github.com/events
Successfully fetched 30 events.
First event payload snippet (action): N/A
Important Note:
If the .env file contains secret API keys, we should add it in .gitinore file to prevent check-in.
.env
Project Structure Summary
Your final project directory structure will look like this:
simple-python-project/
├── .venv/ # Virtual environment
├── .env # API configuration & secrets
├── .gitignore # Files to exclude from Git
├── main.py # Main execution logic
├── README.txt # Project instructions
└── requirements.txt # List of dependencies