python - Import a module from the root -
python - Import a module from the root -
in structure:
app -companies --models.py --__init__.py -manage.py -__init__.py
models.py
class company(): pass
manage.py
from flask import flask app = flask(__name__) app.companies.models import company if __name__ == '__main__': manager.run() importerror: no module named app.companies.models
what reason? in views.py, within companies folder, import works without problem.
the problem not code, how you're running it:
i running python manage.py
for work, working directory must app
. current working directory automatically added sys.path
. there 2 possibilities here, both of them bad:
the more directory containing app
isn't on sys.path
@ all, far python concerned, there no top-level bundle named app
.
the less directory containing app
on sys.path
, app
itself. in case, confuse hell out of importer, because same file or directory can end imported 2 or more separate objects different names in sys.modules
. it's hard predict problems can cause, cause problems.
either way, solution not run way. instead, run 1 directory up, directory app
in. means have run 1 of next ways:
python -m app.manage python app/manage.py
python python-2.7 flask
Comments
Post a Comment