Skip to content

Latest commit

 

History

History
239 lines (167 loc) · 5.96 KB

File metadata and controls

239 lines (167 loc) · 5.96 KB

Executing Python Projects as Directories

Slide 1: Executing Python Projects as Scripts

Python projects are typically run by invoking a specific script file. However, there's a more elegant and user-friendly approach: executing the entire project directory as a script. This method simplifies project execution and improves accessibility for other users.

my_project/
    ├── main.py
    ├── module1.py
    └── module2.py

# Execution
python my_project

Slide 2: The Magic of main.py

The key to this approach is renaming your project's entry point to __main__.py. This special filename allows Python to treat the directory as an executable package.

from module1 import function1
from module2 import function2

def main():
    function1()
    function2()

if __name__ == "__main__":
    main()

Slide 3: How It Works

When you execute a directory, Python looks for a __main__.py file within it. If found, this file is treated as the entry point for the entire project.

python my_project

# Equivalent to
python my_project/__main__.py

Slide 4: Advantages of Directory Execution

This approach offers several benefits:

  1. Simplified execution
  2. Improved project organization
  3. Better user experience for collaborators
python /path/to/project/specific_script.py

# Directory execution method
python /path/to/project

Slide 5: Setting Up Your Project

To implement this method, follow these steps:

  1. Create a project directory
  2. Rename your main script to __main__.py
  3. Organize other modules within the directory
my_cool_project/
    ├── __main__.py
    ├── module1.py
    ├── module2.py
    └── data/
        └── input.csv

Slide 6: main.py Structure

The __main__.py file serves as the entry point and typically contains:

  1. Imports from other project modules
  2. A main function defining the program's logic
  3. A conditional block to run the main function
from module1 import process_data
from module2 import generate_report

def main():
    data = process_data('data/input.csv')
    generate_report(data)

if __name__ == "__main__":
    main()

Slide 7: Importing Project Modules

When using directory execution, you can import other modules within your project using relative imports.

from .module1 import function1
from .module2 import function2

def main():
    result = function1()
    function2(result)

if __name__ == "__main__":
    main()

Slide 8: Real-Life Example: Data Processing Pipeline

Let's create a simple data processing pipeline using this approach.

from .data_loader import load_data
from .processor import process_data
from .visualizer import visualize_results

def main():
    raw_data = load_data('data/raw_data.csv')
    processed_data = process_data(raw_data)
    visualize_results(processed_data)

if __name__ == "__main__":
    main()

# Result: The pipeline runs, processing data and generating visualizations

Slide 9: Real-Life Example: Web Scraping Project

Here's how you might structure a web scraping project using directory execution.

from .scraper import scrape_website
from .parser import parse_data
from .storage import save_to_database

def main():
    raw_html = scrape_website('https://example.com')
    parsed_data = parse_data(raw_html)
    save_to_database(parsed_data)

if __name__ == "__main__":
    main()

# Result: The scraper runs, collecting, parsing, and storing data from the website

Slide 10: Handling Command-Line Arguments

You can easily incorporate command-line arguments in your __main__.py file using the argparse module.

import argparse
from .processor import process_data

def main():
    parser = argparse.ArgumentParser(description='Process some data.')
    parser.add_argument('input_file', help='Input file to process')
    parser.add_argument('--output', help='Output file name')
    args = parser.parse_args()

    process_data(args.input_file, args.output)

if __name__ == "__main__":
    main()

# Usage: python my_project input.csv --output results.csv

Slide 11: Testing Considerations

When using directory execution, it's important to structure your tests appropriately. Place test files outside the main package directory.

    ├── __main__.py
    ├── module1.py
    ├── module2.py
    └── tests/
        ├── test_module1.py
        └── test_module2.py

# Running tests
python -m unittest discover tests

Slide 12: Packaging and Distribution

Directory execution works well with Python's packaging tools. Create a setup.py file to make your project installable and executable.

from setuptools import setup, find_packages

setup(
    name='my_cool_project',
    version='0.1',
    packages=find_packages(),
    entry_points={
        'console_scripts': [
            'my_cool_project=my_cool_project.__main__:main',
        ],
    },
)

# Install: pip install .
# Run: my_cool_project

Slide 13: Best Practices and Considerations

When using directory execution:

  1. Keep __main__.py focused on high-level logic
  2. Use relative imports for project modules
  3. Consider adding a __init__.py file in your project root
  4. Document the execution method in your README file
from .module1 import *
from .module2 import *

# This allows users to import from your package more easily
# from my_cool_project import some_function

Slide 14: Additional Resources

For more information on Python project structure and best practices:

  1. "Python Packaging User Guide" - https://packaging.python.org/
  2. "Structuring Your Project" - https://docs.python-guide.org/writing/structure/
  3. "Python Application Layouts: A Reference" - https://realpython.com/python-application-layouts/

These resources provide in-depth information on Python project organization, packaging, and distribution.