Is there something I am missing with the new `pref...
# ask-community
p
Is there something I am missing with the new
prefect run
CLI
--module
option? • Doing
python -m flows.myflow
works • Doing
prefect run -m flows.myflow
raises
No module named 'flows'
k
Hi @Pierre Monico, is
flows
installed as a Python module with
pip
?
p
Nope, it’s just a folder (containing
__init__.py
) inside the repo
And containing the flow sources obviously
k
I think it needs to be installed as a module as in
pip install -e .
so that it can be imported from whatever directory you’re in. Are you familiar with how to create the
setup.py
?
z
We use relatively simple code to do this import
Copy code
import importlib
from typing import Any


def import_object(name: str) -> Any:
    """Import an object given a fully-qualified name.

    Args:
        - name (string): The fully-qualified name of the object to import.

    Returns:
        - obj: The object that was imported.

    Example:

    ```python
    >>> obj = import_object("random.randint")
    >>> import random
    >>> obj == random.randint
    True
""" # Try importing it first so we support "module" or "module.sub_module" try: module = importlib.import_module(name) return module except ImportError: # If no subitem was included raise the import error if "." not in name: raise # Otherwise, we'll try to load it as an attribute of a module mod_name, attr_name = name.rsplit(".", 1) module = importlib.import_module(mod_name) return getattr(module, attr_name)```
It should work for modules that are on your
PYTHONPATH
even if they have not been installed with pip
p
Thanks for the help and code!
Frankly speaking it would be great if it would just work when running the command from the repo - just like
python -m
as a matter of fact.
But not too important, I was just testing it out - probably will rather make use of Docker Storage.
z
I am unsure why it is not 🙂 I agree that'd be nice
👀 1
p
BTW, I just copy-pasted the code above at the root of my repo (where running
python -m
and
prefect run -m
) and executed the function with the same argument and guess what: it works.
z
Interestingly, I just tested
import mod
importlib.import_module("mod")
and
python -m mod
with a simple folder and it worked.
Must be something weird going on with the run CLI, which doesn't really make sense to me but we can look into it
p
For reference, just in case that has some impact on it: I am using
pipenv
and running this inside a virtual environment.
Quick and dirty but I just pushed this in case it helps: https://github.com/pierremonico/prefect-cli-example