简体   繁体   中英

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.

You need to add __init__.py to every directory that you want to treat as a Python package, not just to the root directory.

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.

You could place your tests in the Project directory itself, parallel to api and utils.

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!

It may be helpful if you look at these three tutorials. It may not completely solve what you imagined, but it will certainly give you an idea or background on how to do it.

They have already written about Django , so you can see the details at: Best practice for Django project working directory structure .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM