Python Submodule Importing Madness

前端 未结 2 1273
误落风尘
误落风尘 2021-01-17 05:01

I\'m banging my head against the wall with some basic Python importing. I have simplified the problem as much as possible hoping I\'d be able to expand this to a larger scal

2条回答
  •  囚心锁ツ
    2021-01-17 05:07

    Python makes it intentionally difficult to mix scripts in a modules. The way this can be handled is like this:

    /project
        /scripts
            run.py
        /sandbox
            __init__.py
            /p1
                __init__.py
                file1.py
                file2.py
    

    /project/sandbox/p1/file1.py

    from .file2 import B
    class A(object):
        pass
    

    /project/sandbox/p1/file2.py

    class B(object):
        pass
    

    /project/scripts/run.py

    from sandbox.p1.file1 import A
    a = A()
    

    I make sure that the path to /project is in a .pth file in the site-packages of the virtual environment I want to use it from.

    conda

    If you use conda, you can use conda develop

提交回复
热议问题