Get Pycharm to see dynamically generated python modules

橙三吉。 提交于 2019-12-04 19:58:22

问题


Using Apache airflow they install plugins by modifying sys.modules like this:

 sys.modules[operators_module.__name__] = operators_module

Which is how they get python classes from their plugins folder to be imported via

from airflow.operators.plugin_name import Operator

even though the class Operator exists inside

airflow/plugins/Operators.py

This makes it impossible for PyCharm to understand the above import statement because it is a non-traditional way of generating module/module name.

Is there any way to get PyCharm to have something like a module/package alias for situations like these?


回答1:


Airflow loads plugins by searching the plugins directory for AirflowPlugin subclasses, then binding the operators etc. stored in attributes of those subclasses to new modules under the airflow namespace.

Resolving code transforming classes into modules is beyond PyCharm's capabilities, so unless you want to override this behavior by adding the plugins directory to sys.path and importing the contents directly, the best solution is to whitelist the modules airflow creates through Settings -> Editor -> Inspections -> Python -> Unresolved references and adding airflow.operators.plugin_name to the Ignore references list. Then, using the module like so:

from airflow.operators import plugin_name
task = plugin_name.Operator()

will no longer raise any errors, although you still won't get the benefits of code completion.




回答2:


If your plugin is in the same repository as the dag you can wrap you import in try-except block:

try:
   # local development
   from plugin_name import Operator
except ImportError:
   from airflow.operators.plugin_name import Operator

my_operator = Operator(...)

And mark plugins directory as sources in your PyCharm Project Structure.
Afterwards PyCharm picks up your plugin source code and works nicely with autocompletion and so on.



来源:https://stackoverflow.com/questions/43380679/get-pycharm-to-see-dynamically-generated-python-modules

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!