bottlepy / bottle

bottle.py is a fast and simple micro-framework for python web-applications.
http://bottlepy.org/
MIT License
8.44k stars 1.46k forks source link

relative import does not work when using reloader #418

Open flytwokites opened 11 years ago

flytwokites commented 11 years ago

structure:

pkg
  + __init__.py
  + main.py
  + util.py

FILE: pkg.main.py

from .util import * # this line will raise a ValueError
if __name__ == '__main__':
    run(..., reload=True)
[~]$ python -m pkg.main
ValueError: Attempted relative import in non-package

The relative import will be fail because __package__ is not set. bottle version: 0.11.4

jpo commented 11 years ago

I was able to get this to work by creating a run_server.py file outside of the pkg directory. My directory structure looks like this:

run_server.py
pkg/
  + __init__.py
  + main.py
  + util.py

Here is the code for run_server.py:

from pkg import main
main.run(host='localhost', port=5000, reloader=True)