Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoreload can't handle module / __main__ #1411

Closed
ghost opened this issue Mar 19, 2016 · 4 comments · Fixed by #1450
Closed

Autoreload can't handle module / __main__ #1411

ghost opened this issue Mar 19, 2016 · 4 comments · Fixed by #1450

Comments

@ghost
Copy link

ghost commented Mar 19, 2016

Originally reported by: Anonymous


I try to run python -m mymodule (and mymodule/ contains __main__.py). The module then spawns a CherryPy server. Everything works until it tries to reload itself, where it tries to run /path/to/python mymodule/__main__.py, without importing mymodule. This makes my module to throw various ImportErrors that claim mymodule cannot be found.

Could you share any tips how to organize the code, or change the way I run the server, to make reloading work?


@jaraco
Copy link
Member

jaraco commented May 1, 2016

My guess is that the autoloading feature is simply not aware of the -m entry technique. I was not aware that python -m foo would implicitly import the package.

$ mkdir foo
$ cat > foo/__main__.py
print("Ran __main__")
import foo
$ python -m foo
Ran __main__
$ cat > foo/__init__.py
print("imported foo")
$ python -m foo
imported foo
Ran __main__

I'm pretty sure you can work around the issue by adding import mymodule in your __main__. Did you try that? If that doesn't work, what happens?

@sevmonster
Copy link

sevmonster commented May 28, 2016

Didn't see that this had an issue already.

See #1436, both issues are caused by the same situation.
This bug has been around for a few years now.

@jaraco
Copy link
Member

jaraco commented May 28, 2016

I suspect that the autoreloader functionality has never supported launching as -m, which I believe is the underlying issue here. To work around the issue, you probably need to avoid launching using -m.

If someone would like to devise a fix, I suspect using the runpy module, mentioned in PEP 338 might be way to do it.

@batterseapower
Copy link
Contributor

Bit tricky to fix because of http://bugs.python.org/issue14208

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Bugs
Fixed
3 participants