Cannot find reference 'xxx' in __init__.py - Python / Pycharm

后端 未结 7 848
一整个雨季
一整个雨季 2020-12-01 01:09

I have a project in Pycharm organized as follows:

-- Sources
   |--__init__.py
   |--Calculators
      |--__init__.py
      |--Filters.py
   |--Controllers
         


        
7条回答
  •  鱼传尺愫
    2020-12-01 01:37

    You should first take a look at this. This explains what happens when you import a package. For convenience:

    The import statement uses the following convention: if a package’s __init__.py code defines a list named __all__, it is taken to be the list of module names that should be imported when from package import * is encountered. It is up to the package author to keep this list up-to-date when a new version of the package is released. Package authors may also decide not to support it, if they don’t see a use for importing * from their package.

    So PyCharm respects this by showing a warning message, so that the author can decide which of the modules get imported when * from the package is imported. Thus this seems to be useful feature of PyCharm (and in no way can it be called a bug, I presume). You can easily remove this warning by adding the names of the modules to be imported when your package is imported in the __all__ variable which is list, like this

    __init__.py

    from . import MyModule1, MyModule2, MyModule3
    __all__ = [MyModule1, MyModule2, MyModule3]
    

    After you add this, you can ctrl+click on these module names used in any other part of your project to directly jump to the declaration, which I often find very useful.

提交回复
热议问题