简体   繁体   English

Python:无法导入模块

[英]Python: Unable to import module

I am learning flask from https://blog.miguelgrinberg.com/ 我正在从https://blog.miguelgrinberg.com/学习烧瓶

I have application microblog with filename as microblog.py 我有应用程序微博,文件名为microblog.py

from app import app

and I have directory named app and it contains __init__.py with below code 而且我有一个名为app目录,它包含带有以下代码的__init__.py

from flask import Flask
from config import Config

app = Flask( __name__ )
app.config.from_object(Config)
from app import routes

But when I run flask run I am getting error as 但是当我运行flask run我得到了错误

ImportError: No module named 'app'

I understand like if I want to indicate a directory as a package then I have to include a __init__.py inside the directory and I did so for app directory. 我了解,例如,如果我想将目录指示为软件包,则必须在目录内包含__init__.py ,而对于app目录则必须这样做。

Directory structure 目录结构

.
├── __init__.py
├── app
│   ├── __init__.py
│   ├── forms.py
│   ├── routes.py
│   └── templates
│       ├── base.html
│       ├── index.html
│       └── login.html
├── config.py
└── microblog.py

Full stacktrace 完整的堆栈跟踪


flask run                                       
 * Serving Flask app "microblog.py"
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
Usage: flask run [OPTIONS]

Error: While importing "microblog.microblog", an ImportError was raised:

Traceback (most recent call last):
  File "~/anaconda3/envs/flask_python3.5.2/lib/python3.5/site-packages/flask/cli.py", line 235, in locate_app
    __import__(module_name)
  File "~/learning/migual_flask/microblog/microblog.py", line 1, in <module>
    from app import app
ImportError: No module named 'app'

And this code worked till recently and suddenly started giving troubles. 这段代码一直工作到最近,突然开始引起麻烦。 I am unable to find where this is going wrong. 我无法找到问题所在。

Any help greatly appreciated. 任何帮助,不胜感激。

Thanks. 谢谢。

Okay, from what you post with the hierarchy, it's clear that it should work, but I suspect that you were importing from wrong directory. 好的,根据您在层次结构中发布的内容,很明显它应该可以工作,但是我怀疑您是从错误的目录导入的。

Given the directory: 给定目录:

.
├── __init__.py
├── app
│   ├── __init__.py
│   ├── forms.py
│   ├── routes.py
│   └── templates
│       ├── base.html
│       ├── index.html
│       └── login.html
├── config.py
└── microblog.py

Assume you import them from root directory (the "." at the top): 假设您从根目录(顶部的“。”)导入它们:

$ls                                                                                                                                                        
__init__.py  app          config.py    microblog.py

$ls app
__init__.py  forms.py     routes.py

# which works
>>> import config
>>> import microblog
>>> import app

# and
>>> from app import routes
>>> from app import forms

I noticed that you have app = Flask() in your app/__init__.py , which is a very bad practice which should be avoided as much as possible (what makes you think it's good? I can't think of any), but it works for me: 我注意到您的app/__init__.pyapp = Flask() ,这是一个非常糟糕的做法,应尽可能避免(这使您认为这很好,我想不到),但是这个对我有用:

>>> import app
>>> from app import app

# the first app is module
# the second is app = Flask()
# but the second one will overwrite the first one's name
# which is another terrible practice

And you can inspect a module: 您可以检查模块:

>>> import app
>>> help(app) # app = Flask() is there

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM