Skip to main content

Python Project Essentials

Developing a Python application involves much more than writing code. Modern Python projects require proper project organization, dependency management, configuration handling, and communication with external services. In this module, you’ll learn the essential tools and practices used in professional Python development.

Topics Covered

In this module, you’ll learn:
  1. Project Structure
  2. Virtual Environments
  3. Dependency Management with uv
  4. Environment Variables and Configuration
  5. Working with JSON Data
  6. Consuming REST APIs
  7. Dependency Injection Concepts
  8. Best Practices
Try Yourself: 💻 VS Code | 🚀 Colab | 📥 Download
Verify Solutions: 💻 VS Code | 🚀 Colab | 📥 Download
By the end of this module, you’ll be able to organize Python projects, manage dependencies efficiently, configure applications securely, exchange JSON data, consume REST APIs, and understand the fundamentals of dependency injection.

Project Structure

A well-organized project is easier to understand, maintain, and extend. A typical Python project might look like this.

Common Files

As your project grows, organizing code into separate modules and packages improves readability and maintainability.

Exercise 1

Create the following project structure. Expected Structure
Create the folders and files using your preferred editor or the terminal.

Exercise 2

Identify the purpose of each file.

Virtual Environments

A virtual environment is an isolated Python environment for a project. It allows each project to have its own:
  • Python packages
  • Package versions
  • Dependencies
Without virtual environments, installing a package for one project affects every project on your system.

Creating a Virtual Environment

Using the built-in venv module.
Activate it. Windows
macOS / Linux
Deactivate the environment.

Why Use Virtual Environments?

  • Prevent dependency conflicts.
  • Keep projects isolated.
  • Make projects reproducible.
  • Simplify dependency management.

Exercise 1

Create and activate a virtual environment for a new project.
(macOS/Linux)or
(Windows)

Exercise 2

Deactivate the virtual environment. Expected Command

Dependency Management with uv

uv is a modern Python package and project manager. It is significantly faster than traditional tools such as pip because it is implemented in Rust.

Installing uv

Verify the installation.

Creating a Project

This creates a new project with a standard structure.

Adding a Package

The dependency is automatically added to the project configuration.

Installing Dependencies

This installs all dependencies listed in pyproject.toml.

Removing a Package

Running Python

Why Use uv?

  • Fast dependency resolution.
  • Modern project management.
  • Automatic virtual environment creation.
  • Reproducible builds.
  • Simple dependency synchronization.

Exercise 1

Create a new project named student-management using uv. Expected Command

Exercise 2

Add the requests package and synchronize the project. Expected Commands

Environment Variables

Environment variables allow you to store configuration values outside your source code. They are commonly used to store:
  • API Keys
  • Database URLs
  • Secret Keys
  • Application Settings
Instead of writing sensitive information directly in your code,
store it in a .env file.
This keeps sensitive information separate from your application.

Reading Environment Variables

Install the package.
Create a .env file.
Read the value.
Output ?

Providing Default Values

Use a default value when an environment variable is missing.
Output ?

Why Use Environment Variables?

  • Keep secrets out of source code.
  • Use different configurations for development and production.
  • Improve application security.
  • Simplify deployment.

Exercise 1

Create a .env file containing:
  • APP_NAME
  • PORT
Read and display both values. Expected Output

Exercise 2

Read an environment variable named HOST. If it doesn’t exist, display "localhost". Expected Output

Configuration Management

As projects grow, configuration values increase. Instead of scattering configuration throughout the application, place them in one location. Example project.
config.py
main.py
This approach centralizes application configuration and makes maintenance easier.

Best Practices

  • Never hardcode secrets.
  • Use .env for local development.
  • Keep configuration in one module.
  • Add .env to .gitignore.

Exercise 1

Create a config.py file that loads:
  • APP_NAME
  • DEBUG

Exercise 2

Use config.py in another Python file to display the application name.

Working with JSON Data

JSON (JavaScript Object Notation) is the most widely used format for exchanging data between applications. A JSON document consists of key-value pairs. Example JSON.
Python provides the built-in json module for working with JSON.

Converting Python Objects to JSON

Use json.dumps().
Output ?

Converting JSON to Python Objects

Use json.loads().
Output ?

Reading JSON from a File

Suppose student.json contains
Read the file.
Output ?

Writing JSON to a File

This creates a formatted JSON file.

Exercise 1

Convert the following dictionary into JSON.

Exercise 2

Read the following JSON string and display the employee name.
Expected Output

Consuming REST APIs

Modern applications frequently communicate with external services using REST APIs. Some common examples include:
  • Weather applications
  • Payment gateways
  • AI services
  • Maps and location services
  • Social media platforms
Python provides several libraries for consuming REST APIs. One of the most popular is requests. Install it using uv.

Making a GET Request

Let’s fetch sample user information from the JSONPlaceholder API.
Output ?
A status code of 200 indicates that the request was successful.

Reading JSON Response

Most REST APIs return JSON.
Output ?

Sending Query Parameters

Query parameters provide additional information to the server.
Output ?
The generated URL becomes

Sending Headers

Headers provide additional metadata such as API keys.
Many real-world APIs require authentication through headers.

Making a POST Request

Output ?
Status code 201 indicates that a new resource was created.

Common HTTP Methods

Exercise 1

Fetch user 5 from JSONPlaceholder and display the user’s name. Expected Output

Exercise 2

Create a new post using the JSONPlaceholder API. Expected Output

Dependency Injection Concepts

As applications grow, classes often depend on other classes. For example, an application may have:
  • Database Service
  • Email Service
  • Notification Service
  • Authentication Service
If one class creates its own dependencies, the code becomes tightly coupled.

Without Dependency Injection

Output ?
Here, NotificationService directly creates an EmailService. Replacing the email service later becomes difficult.

With Dependency Injection

Instead of creating the dependency inside the class, pass it from outside.
Output ?
The dependency is now injected from outside. This makes the code:
  • Easier to test
  • Easier to maintain
  • More reusable
  • Less tightly coupled

Another Example

Output ?
Notice that NotificationService did not change. Only the dependency changed. This is the primary advantage of Dependency Injection.

Exercise 1

Create a PaymentService class and inject it into an OrderService. Expected Output

Exercise 2

Create both EmailService and SMSService and inject each into NotificationService. Expected Output

Best Practices

  • Organize projects into logical modules and packages.
  • Use virtual environments for every project.
  • Manage dependencies with uv.
  • Store secrets in environment variables.
  • Keep configuration separate from source code.
  • Use JSON for data exchange.
  • Handle API responses and errors gracefully.
  • Prefer dependency injection over creating dependencies inside classes.

Practice

To reinforce what you’ve learned in this section, practice with the interactive follow-along notebook:

Follow-Along Practice

Practice dependency management using uv, reading environment variables from .env, parsing/generating JSON payloads, calling REST APIs using requests, and implementing dependency injection.💻 VS Code | 🚀 Colab | 📥 Download

Summary

In this module, you learned the essential tools and practices used in modern Python projects.

Key Concepts Covered

  • Project Structure
  • Virtual Environments
  • Dependency Management with uv
  • Environment Variables
  • Configuration Management
  • Working with JSON
  • Consuming REST APIs
  • Dependency Injection Concepts
  • Best Practices
These concepts form the foundation of professional Python development and are widely used in frameworks such as FastAPI, Django, and Flask.