How can I run Prefect and its commands with 'python -m prefect ...'?

Hello, I have a machine at work where I can’t simply run prefect server start for example. (I get blocked when running .exe) So, I’m attempting to run python -m prefect server start but when I do, I receive the following error:

No module named prefect.__main__; 'prefect' is a package and cannot be directly executed

So, I created a __main__.py file and inserted the following lines:

if __name__ == '__main__':
    main()

Then, I run python -m prefect server start again, I get the following error:

 line 3, in <module>
    main()
NameError: name 'main' is not defined. Did you mean: 'min'?

I’m not sure what I need to put in __main__.py to be able to run the file with python -m prefect server start

Any help is appreciated. Thank you.

1 Like

I have exactly the same challenge. I am replying to let you know that you are not alone in this. Immediately I get a solution, I will let you know

If you can use the -c option, then you could do something like this:

python -c "from prefect.cli import app; app()" server start

Alternatively, you can stick the code in a file and run that:

# cli.py
from prefect.cli import app
app()

Then:

python -m cli server start
1 Like

Here is how I was able to fix mine.
I had to create a new conda environment.
Then I installed prefect using pip install -U prefect
I started the server using prefect server start and it worked.
Then I installed other necessary packages from the requirement file.
Everything worked just fine. I am able to access prefect CLI from my terminal