Import a module from a directory (package) one level up

前端 未结 4 948
抹茶落季
抹茶落季 2021-01-04 08:35

What is the right way in Python to import a module from a directory one level up? The directory is a Python package with all these modules and I have a sub directory with co

相关标签:
4条回答
  • 2021-01-04 08:50

    If you're having issues you can use the following also.

    This imports from current directory

    from . import something
    

    this import from one directory above

    from .. import fruit
    

    Doc for relative path: https://docs.python.org/2/tutorial/modules.html#intra-package-references

    RELATIVE PACKAGE ISSUE ValueError: Attempted relative import in non-package

    For people having issues with the relative package. Here's your solution.

    if __name__ == "__main__" and __package__ is None:
        __package__ = "expected.package.name"
    

    Also the explanation, copied from another python docs

    When the main module is specified by its filename, then the package attribute will be set to None . To allow relative imports when the module is executed directly, boilerplate similar to the following would be needed before the first relative import statement:

    if __name__ == "__main__" and __package__ is None:
        __package__ = "expected.package.name
    

    Note that this boilerplate is sufficient only if the top level package is already accessible via sys.path . Additional code that manipulates sys.path would be needed in order for direct execution to work without the top level package already being importable.

    Doc for creating packages I'm not going to paste the content since it's rather long, but here's the section on the python docs for creating packages. https://docs.python.org/2/tutorial/modules.html#packages

    One more docs on PYTHONPATH https://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH

    0 讨论(0)
  • 2021-01-04 08:52

    In your main file recipe.py add pkg1 path to the PYTHONPATH

    sys.path.append('/path/to/pkg1')
    

    Or use a relative path

    sys.path.append('../..')
    

    This should allow importing pkg1 from any where in your program.

    There is always the option of using relative imports as suggested here, I find using absolute imports more readable and less likely to lead to bugs. Also, in large projects using relative imports will have you constantly calculating path hierarchies while when using absolute imports, it's simple to know you always refer to one root directory.

    About relative imports from PEP328 in Python 2.5:

    Reading code which relies on relative imports is also less clear, because a reader may be confused about which module is intended to be used. Python users soon learned not to duplicate the names of standard library modules in the names of their packages’ submodules, but you can’t protect against having your submodule’s name being used for a new module added in a future version of Python.


    Guido is suggesting using leading dots in relative imports to avoid ambiguous imports as described above, from PEP328 again:

    Guido has Pronounced that relative imports will use leading dots. A single leading dot indicates a relative import, starting with the current package. Two or more leading dots give a relative import to the parent(s) of the current package, one level per dot after the first.

    0 讨论(0)
  • 2021-01-04 09:00

    You can use relative imports:

    from ..fruit import Fruit
    
    0 讨论(0)
  • 2021-01-04 09:04

    Instead of using upper imports, have an entry point that you can call from the upper most level and have your imports relative to that entry point in your recipe.py file.

    Ex:

    pkg1/
       __init__.py  
       main.py
       fruit.py  
       +sub_pkg
          __init__.py
          recipe.py
    

    Where main.py contains:

    #!/usr/bin/env python3
    
    import subpkg.recipe as recipe
    import fruit
    
    if __package__ is None and __name__ == "__main__":
        __package__ = "main"
        print()
        # Call recipe.py or fruit.py methods from here
    

    And in recipe.py:

    import fruit
    # do stuff with fruit module, which is properly imported now
    

    To run, call python3 main.py

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