30 Days of Python: Day 25 Packaging

I’m making a small project every day in python for the next 30 days (minus some vacation days). I’m hoping to learn many new packages and  make a wide variety of projects, including games, computer tools, machine learning, and maybe some science. It should be a good variety and I think it will be a lot of fun.

Day 25: Packaging

Today’s project was learning about packages in Python. I refactored my code for both the utilities project and the games projects to use packages. To make a package in python you simply add an __init__.py file to a directory. The resulting package will have the same name as the directory did. And this is why I had to refactor my code; I didn’t want packages named src. Originally, my code was just in the src directory in the eclipse projects:

#The original projects:
utilities/
    src/
        cat.py
        du.py
        ...
        tail.py
        wc.py
    resources/
    README.md

games/
    src/
        breakout.py
        fifteen.py
        menu.py
        tetris.py
        ...
        sprite.py
        simplegui.py
    lib/
    README.md

The new version has one package in the utilities project (utilities) and two in the games (game_tools and games):

#The refactored projects:
utilities/
    utilities/
        __init__.py
        cat.py
        du.py
        ...
        tail.py
        wc.py
    resources/
    README.md

games/
    src/
        game_tools/
            __init__.py
            sprite.py
            simplegui.py
        games/
            __init__.py
            breakout.py
            fifteen.py
            tetris.py
            ...
        menu.py
    lib/
    README.md

I had to keep menu.py at the top level if I wanted to call it directly (otherwise it has to be called from inside games and the absolute paths are all wrong). It’s still a work in progress but I can use import statements in interactive mode to pull in the various modules and start up the games. If you know of any good links that explain project setup, feel free to leave them in the comments!

For those who are interested here’s my github repo for the games project: https://github.com/robb07/python_games

And here’s my desert island utilities repository: https://github.com/robb07/utilities

 

Leave a comment