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
bb-migration opened this Issue Mar 19, 2016 · 4 comments

Comments

4 participants
@bb-migration

bb-migration 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

This comment has been minimized.

Show comment
Hide comment
@jaraco

jaraco May 1, 2016

Member

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?

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?

@ds6

This comment has been minimized.

Show comment
Hide comment
@ds6

ds6 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.

ds6 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

This comment has been minimized.

Show comment
Hide comment
@jaraco

jaraco May 28, 2016

Member

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.

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

This comment has been minimized.

Show comment
Hide comment
@batterseapower

batterseapower Jul 1, 2016

Contributor

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

Contributor

batterseapower commented Jul 1, 2016

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