python packaging for relative imports

前端 未结 3 992
小蘑菇
小蘑菇 2020-11-29 19:18

First off all: I\'m sorry, I know there has been lots of question about relative imports, but I just didn\'t find a solution. If possible I would like to use the following d

相关标签:
3条回答
  • 2020-11-29 19:33

    After hours of searching last night I found the answer to relative imports in python!! Or an easy solution at the very least. The best way to fix this is to have the modules called from another module. So say you want demo.py to import myClass.py. In the myClass folder at the root of the sub-packages they need to have a file that calls the other two. From what I gather the working directory is always considered __main__ so if you test the import from demo.py with the demo.py script, you will receive that error. To illustrate:

    Folder hierarchy:

    myClass/
        main.py #arbitrary name, can be anything
        test/
            __init__.py
            demo.py
        src/
            __init__.py
            myClass.py
    

    myClass.py:

    def randomMaths(x):
        a = x * 2
        y = x * a
        return y
    

    demo.py:

    from ..src import myClass
    
    def printer():
        print(myClass.randomMaths(42))
    

    main.py:

    import test.demo
    
    demo.printer()
    

    If you run demo.py in the interpreter, you will generate an error, but running main.py will not. It's a little convoluted, but it works :D

    0 讨论(0)
  • 2020-11-29 19:34

    Intra-package-references describes how to myClass from test/*. To import the package from outside, you should add its path to PYTHONPATH environment variable before running the importer application, or to sys.path list in the code before importing it.

    Why from ..src import myClass fails: probably, src is not a python package, you cannot import from there. You should add it to python path as described above.

    0 讨论(0)
  • 2020-11-29 19:44

    ValueError: Attempted relative import in non-package

    Means you attempt to use relative import in the module which is not package. Its problem with the file which has this from ... import statement, and not the file which you are trying to import.

    So if you are doing relative imports in your tests, for example, you should make your tests to be part of your package. This means

    1. Adding __init__.py to test/
    2. Running them from some outside script, like nosetests

    If you run something as python myClass/test/demo.py, relative imports will not work too since you are running demo module not as package. Relative imports require that the module which uses them is being imported itself either as package module, from myClass.test.demo import blabla, or with relative import.

    0 讨论(0)
提交回复
热议问题