Search code examples
pythondjangosetup-projectproject-structure

Best way to organize a python project to easily access all of the modules


I know this question has been asked in a lot of places, but none of them really seem to answer my question.

I am creating a standalone application in python, my current project structure is like so:

Project/
    utils/
        log.py
    api/
        get_data_from_api.py
    main.py

I would like to have a set up similar to django, in which I can refer to any file by using a Project.<package> syntax.

For example, if I wanted to access my log module from my get_data_from_api module, I could do something like this:

get_data_from_api.py

import Project.utils.log

# Rest of code goes here...

However, I can't seem to get that to work, even when I added an __init__.py file in the root directory.

I read somewhere that I should modify my PYTHONPATH, but I would like to prevent that, if possible. Additionally, django seemed to pull it off, as I couldn't find any PYTHONPATH modification code in there.

I really appreciate the help!

Post Note: Where would tests fit in this file structure? I would like them to be separate, but also have access to the entire project really easily.


Solution

  • I ended up using a file structure like the following:

    src/
        main.py
        project/
            api/
                get_data_from_api.py
            util/
                log.py
        test/
    

    That way, because I am running the main.py file, from anywhere in the program, I can just do import project.<package>.<module>. For example, I can just do this:

    get_data_from_api.py

    import project.util.log
    
    # Rest of code goes here...
    

    And everything works!

    If I'm completely shooting in the dark please let me know, I'd much rather be wrong now then hundreds of hours into the project!