If you’re familiar with PHP’s Composer and its project structure convention, you’ll find Python’s approach both similar and distinct. In this section, we’ll walk through the key steps to set up a Python project, focusing on virtual environments, dependency management, and project structure—concepts that will feel familiar if you’re coming from PHP.


Why Virtual Environments Matter

In PHP, Composer installs dependencies in a vendor/ directory within your project, isolating them from the global system. Python achieves a similar goal using virtual environments. These are self-contained directories that contain all the dependencies and Python versions required for a project, ensuring that your system-wide Python installation remains clean and uncluttered.

Key benefits of virtual environments:

  • Isolation: Each project has its own dependencies, avoiding conflicts between projects.
  • Reproducibility: You can share a project with others, and they can recreate the exact environment using tools like requirements.txt (similar to Composer’s composer.json and composer.lock).
  • Flexibility: You can use different Python versions or packages for different projects without affecting your system.

Step 1: Create a Virtual Environment

Python’s built-in venv module makes it easy to create a virtual environment. Here’s how to do it:

  1. Open a terminal in your project directory.
  2. Run the following command:
python -m venv venv
  • This creates a directory named venv (you can name it anything, but venv is a common convention).
  • The venv directory will contain isolated Python binaries, libraries, and scripts.
  1. Activate the virtual environment:
    • On Windows:
venv\Scripts\activate
  • On macOS/Linux:
source venv/bin/activate
  • Your terminal prompt will now include the name of the virtual environment (e.g., (venv)), indicating it’s active.

Step 2: Install Dependencies

In PHP, you use Composer to install packages. In Python, you use pip, the package installer for Python.

  1. Install a package:
pip install requests
  • This installs the requests library, a popular tool for making HTTP requests in Python.
  1. Install multiple packages:
pip install requests flask numpy
  1. Generate a requirements.txt file:
pip freeze > requirements.txt
  • This file lists all installed packages and their versions, similar to Composer’s composer.lock.
  • To recreate the environment later, run:
pip install -r requirements.txt

Step 3: Project Structure

Python projects typically follow a standard directory structure to keep things organized. Here’s a common layout:

my_project/
├── venv/                # Virtual environment (created above)
├── my_project/          # Main Python package (replace with your project name)
│   ├── __init__.py      # Makes the directory a Python package
│   ├── main.py          # Entry point for your application
│   └── utils.py         # Utility functions
├── tests/               # Unit tests (e.g., using pytest)
├── docs/                # Documentation
├── requirements.txt     # Dependencies
└── README.md            # Project description

Key notes:

  • The my_project/ directory is the root of your Python package. Replace my_project with your actual project name.
  • The __init__.py file is required for Python 3 to recognize a directory as a package (though it can be empty).
  • The tests/ directory is where you’ll place unit tests (similar to PHP’s tests/ directory).

Step 4: Packaging Your Project (Optional)

If you’re developing a reusable Python package (like a library), you can use setup.py to define metadata, dependencies, and entry points. Here’s a basic example:

# setup.py
from setuptools import setup, find_packages

setup(
    name="my_project",
    version="0.1.0",
    packages=find_packages(),
    install_requires=[
        "requests",
        "flask",
    ],
    entry_points={
        "console_scripts": [
            "my_app = my_project.main:main",
        ],
    },
)
  • name: The name of your package.
  • version: The current version of your package.
  • packages: Automatically discovers all packages in your project (using find_packages()).
  • install_requires: Lists dependencies (similar to Composer’s composer.json).
  • entry_points: Defines command-line tools (e.g., my_app is the name of the command).

To install your package in development mode (without publishing it to PyPI), run:

pip install -e .

Best Practices

  • Always use a virtual environment for your projects. It’s the Pythonic way and avoids dependency conflicts.
  • Keep requirements.txt up to date when adding or removing packages.
  • Use setup.py for reusable libraries to define metadata and dependencies.
  • Avoid global pip installs—they can lead to version conflicts. Instead, rely on virtual environments.

Summary

Setting up a Python project is straightforward, especially if you’re familiar with PHP’s Composer and project structure. Key steps include:

  1. Creating a virtual environment with venv.
  2. Installing dependencies with pip and managing them via requirements.txt.
  3. Organizing your project with a standard directory structure.
  4. (Optional) Packaging your project with setup.py for distribution.

By following these steps, you’ll be well on your way to building scalable and maintainable Python applications.