How do you organise a python project that contains multiple packages so that each file in a package can still be run individually?

后端 未结 3 644
醉话见心
醉话见心 2020-12-07 17:35

TL;DR

Here\'s an example repository that is set up as described in the first diagram (below): https://github.com/Poddster/package_problems

I

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-07 18:32

    To run it from both command line and act like library while allowing nosetest to operate in a standard manner, I believe you will have to do a double up approach on Imports.

    For example, the Python files will require:

    try:
        import f
    except ImportError:
        import tools.f as f
    

    I went through and made a PR off the github you linked with all test cases working.

    https://github.com/Poddster/package_problems/pull/1

    Edit: Forgot the imports in __init__.py to be properly usable in other packages, added. Now should be able to do:

    import tools
    tools.c.do_something()
    

提交回复
热议问题