Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google DataFlow/Python: Import errors with save_main_session and custom modules in __main__

Could somebody please clarify the expected behavior when using save_main_session and custom modules imported in __main__. My DataFlow pipeline imports 2 non-standard modules - one via requirements.txt and another one via setup_file. Unless I move the imports into the functions where they get used I keep getting import/pickling errors. Sample error is below. From the documentation, I assumed that setting save_main_session would help to solve this problem, but it does not (see error below). So I wonder if I missed something or this behavior is by design. The same import works fine when placed into a function.

Error:

  File "/usr/lib/python2.7/pickle.py", line 1130, in find_class
    __import__(module)
ImportError: No module named jmespath
like image 618
kpax Avatar asked Jul 12 '18 17:07

kpax


People also ask

What is dataflow in Python?

Pythonflow is a simple implementation of dataflow programming for python. Users of Tensorflow will immediately be familiar with the syntax. At Spotify, we use Pythonflow in data preprocessing pipelines for machine learning models because.

Does Python support dataflow?

Set up your environmentDataflow no longer supports pipelines using Python 2. For more information, see Python 2 support on Google Cloud page. Dataflow doesn't support Python 3.10. Use Python version 3.9 or earlier.


2 Answers

https://cloud.google.com/dataflow/faq#how-do-i-handle-nameerrors https://beam.apache.org/documentation/sdks/python-pipeline-dependencies/

When to use --save_main_session:

you can set the --save_main_session pipeline option to True. This will cause the state of the global namespace to be pickled and loaded on the Cloud Dataflow worker

The setup that best works for me is having a dataflow_launcher.py sitting at the project root with your setup.py. The only thing it does is import your pipeline file and launch it. Use setup.py to handle all your dependencies. This is the best example I've found so far.

https://github.com/apache/beam/tree/master/sdks/python/apache_beam/examples/complete/juliaset

like image 109
Alex Avatar answered Oct 19 '22 11:10

Alex


In particular, --save_main_session fails if your DoFn has an __init__ using super. See https://issues.apache.org/jira/browse/BEAM-6158.

like image 33
chrishmorris Avatar answered Oct 19 '22 13:10

chrishmorris