简体   繁体   中英

Python doesn't find the modules in my package

I must be missing something very basic about building a package in Python. When I create a package following the guidelines of https://docs.python.org/2/tutorial/modules.html#packages and import it, Python does not find any of the modules. For example, say I create the package holygrail with the following structure:

  • holygrail/
    • __init__.py
    • knights.py

I leave __init__.py empty because the docs say that I can and I'm just trying to make a basic package to start. In knights.py I have:

def say():
     print 'Ni!'

If I try import holygrail , Python doesn't give any errors, but holygrail.knights.say() results in Python telling me that the "'module' object [holygrail] has no attribute 'knights'." However, if I specifically import knights via from holygrail import knights , then knights.say() works. In addition, holygrail.knights.say() then also works.

I tried adding the line

__all__  = ['knights']

in the __init__.py file, but this did not change the behavior.

How do I construct a package such that import package loads in all of the modules, allowing statements like package.module.function() ?

Python does not implicitly import the whole package hierarchy. You have to be explicit about what to import on what level of the package using the __init__.py files.

When you set __all__ = ['knights'] in the __init__.py it works only for the import all statements for that module, eg:

>>> import holygrail
>>> holygrail.knights.say()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'knights'

>>> from holygrail import *
>>> knights.say()
Ni!

It can also act as a filter on import all , importing from the module only what's specified.

To get knights automatically imported on import holygrail you have to put import knights or from . import knights from . import knights ( intra-package or relative import ) to the __init__.py . You'll have to do the same for every module explicitly.

Add import knights into __init__.py .

The link you provided does state "In the simplest case, __init__.py can just be an empty file..." Your example is not the simplest case, that's all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM