Search code examples
pythonkedro

kedro run as a python command instead of command line


I am getting started with Kedro, so I created the new kedro project for default iris dataset.

I am able to succesfully run it with kedro run command. My question now is how do I run it as a python command? From the documentation I read that the command kedro run runs the src/project-name/run.py. However, if I run the run.py I get ModuleNotFoundError: No module named 'iris_workflow'. I get the same error if I run the run method from src/project-name/cli.py.

Everything works fine If I run kedro run in terminal.

How do I run kedro run from a python script without subprocess.run(). If I import the run.py or cli.py in a script and run it, I get the same error ModuleNotFoundError: No module named 'iris_workflow'.

This is the default workflow I created with kedro new --starter=pandas-iris


Solution

  • The problem is that your src/ folder which is where your project python package lives isn't on your Python path, so if you modify your PYTHONPATH first, you'll be able to run run.py:

    ~/code/kedro/test-project
    test-project ❯ PYTHONPATH=$PYTHONPATH:$pwd/src python3 src/test_project/run.py
    

    To more concretely answer your question, if you wanted to run Kedro from a Python script, you'd do something like this:

    import sys
    
    sys.path.append("<path-to-your-project-src")
    with KedroSession.create(package_path.name) as session:
        session.run()