nightmare with relative imports, how does pep 366 work?

后端 未结 4 617
無奈伤痛
無奈伤痛 2020-12-13 06:35

I have a \"canonical file structure\" like that (I\'m giving sensible names to ease the reading):

mainpack/

  __main__.py
  __init__.py 

  - helpers/
              


        
4条回答
  •  天涯浪人
    2020-12-13 07:08

    The "boilerplate" given in PEP 366 seems incomplete. Although it sets the __package__ variable, it doesn't actually import the package, which is also needed to allow relative imports to work. extraneon's solution is on the right track.

    Note that it is not enough to simply have the directory containing the module in sys.path, the corresponding package needs to be explicitly imported. The following seems like a better boilerplate than what was given in PEP 366 for ensuring that a python module can be executed regardless of how it is invoked (through a regular import, or with python -m, or with python, from any location):

    # boilerplate to allow running as script directly
    if __name__ == "__main__" and __package__ is None:
        import sys, os
        # The following assumes the script is in the top level of the package
        # directory.  We use dirname() to help get the parent directory to add to
        # sys.path, so that we can import the current package.  This is necessary 
        # since when invoked directly, the 'current' package is not automatically
        # imported.
        parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        sys.path.insert(1, parent_dir)
        import mypackage
        __package__ = str("mypackage")
        del sys, os
    
    # now you can use relative imports here that will work regardless of how this
    # python file was accessed (either through 'import', through 'python -m', or 
    # directly.
    

    If the script is not at the top level of the package directory and you need to import a module below the top level, then the os.path.dirname has to be repeated until the parent_dir is the directory containing the top level.

提交回复
热议问题