gradio: ModuleNotFoundError: No module named '__main__' with auto-reloading

Describe the bug

I’m using rye as a Python package management,here’s the link of documention https://rye-up.com/ Auto-reloading by running rye run gradio src/run.py raised a ModuleNotFoundError

Have you searched existing issues? 🔎

  • I have searched and found no existing issues

Reproduction

rye add gradio
rye sync gradio
rye run gradio src/main.py
# install gradio with rye then run it
# code in hello/src/run.py
import gradio as gr

with gr.Blocks() as app:
    with gr.Row():
        with gr.Column():
            gr.Text("put your pdf file here")
            gr.File()
            gr.Button("submit")
        with gr.Column():
            gr.Button("make")

if __name__ == "__main__":
    app.launch()

Screenshot

No response

Logs

Changes detected in: /Users/code/snaker/hello/src/run.py
Reloading src.run failed with the following exception:
ModuleNotFoundError: No module named '__main__'

System Info

Gradio version: 3.43.2
Python version: 3.11
Macbook M2 MacOS 13.3

Severity

Blocking usage of gradio

About this issue

  • Original URL
  • State: closed
  • Created 10 months ago
  • Reactions: 3
  • Comments: 26 (4 by maintainers)

Most upvoted comments

I encounter this error too ModuleNotFoundError: No module named ‘main

Running version from #6983 I finally got a traceback of what’s causing this issue. It’s the python-dotenv library failing.

  File "C:\Users\.virtualenvs\lib\site-packages\dotenv\main.py", line 336, in load_dotenv
    dotenv_path = find_dotenv()
  File "C:\Users\.virtualenvs\lib\site-packages\dotenv\main.py", line 286, in find_dotenv
    if usecwd or _is_interactive() or getattr(sys, 'frozen', False):
  File "C:\Users\.virtualenvs\lib\site-packages\dotenv\main.py", line 283, in _is_interactive
    main = __import__('__main__', None, None, fromlist=['__file__'])
ModuleNotFoundError: No module named '__main__

I can confirm not using dotenv package i.e not calling load_dotenv() function will resolve the issue. Hopefully this is a thread for devs to pull on and figure out how to fix this bug. For now the only solution is to not use python-dotenvpackage to load .env files. @freddyaboulton

This has been fixed in python-dotenv version 1.0.1!

Adding a new env variable to a dotenv file is picked up by reload mode! Modifying a env variable is not. That’s expected dotenv behavior, it doesn’t overwrite env variables that are already set.

Thanks for your patience everyone.

any progress or ETA on the issue?

Not sure if this is fixable from gradio or is a dotenv bug but it seems for now not using dotenv during development will solve the issue.

@sangv @joaomorossini @MohamedAliRashad Can you see what traceback you get with 4.14.0?

@Nghiauet Please create the demo object outside the if __name__ == "__main__" clause.

Thanks @freddyaboulton for looking into this. I can confirm that I still have this issue with the versions shown below.

pip list | grep ‘gradio’ gradio 4.10.0 gradio_client 0.7.3

Followed by: gradio cc dev ♻ Launching demo/app.py in reload mode

And finally this:

Changed detected in: {my_home_dir_location}/demo/app.py

Reloading demo.app failed with the following exception:

ModuleNotFoundError: No module named ‘main

I’m not sure about rye (what the original issue comment mentions) but I think this problem is fixed in the v4 branch to be released in about two weeks.