Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Pycharm to see dynamically generated python modules

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?

like image 368
jhnclvr Avatar asked Apr 12 '17 22:04

jhnclvr


Video Answer


1 Answers

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.

like image 72
Tautvydas Avatar answered Sep 20 '22 15:09

Tautvydas