[英]ImportError: No module named… in __init__.py file
My project structure is: 我的项目结构是:
/home/giri/couponmonk_project
__init__.py
views.py
__init__.py __init__.py
from flask import Flask
app = Flask(__name__)
app.debug = True
import couponmonk_project.views
views.py views.py
from couponmonk_project import app
@app.route('/', methods = ['GET'])
def index():
return 'Flask is running!'
When I run: 当我跑步时:
gunicorn __init__:app -b localhost:8000
from the folder /home/giri/couponmonk_project
从文件夹/home/giri/couponmonk_project
I get the error: 我得到错误:
ImportError: No module named couponmonk_project.views
I tried adding: 我尝试添加:
import sys
sys.path.append("/home/giri/couponmonk_project")
to my __init__.py
file but still get the same error. 到我的__init__.py
文件,但仍然出现相同的错误。
Even if this had worked, is appending to sys.path
the proper way of doing this? 即使这样做有效,是否将sys.path
追加为执行此操作的正确方法?
I have read that appending to sys
and PYTHONPATH
may not be the best way to fix the issue. 我已经读到,追加到sys
和PYTHONPATH
可能不是解决此问题的最佳方法。
As it has been stated in the above comments you have a circular dependency. 如以上注释中所述,您具有循环依赖关系。 To avoid it, you should make your current couponmonk_project
as module, that will be run by another python script. 为了避免这种情况,您应该将当前的couponmonk_project
为模块,该模块将由另一个python脚本运行。 According to the Flask documentation your project should look like: 根据Flask文档,您的项目应如下所示:
/home/giri/couponmonk_project
__init__.py
/home/giri/couponmonk_project/couponmonk_project
__init__.py
views.py
where inner folder couponmonk_project
is your present project, and outer folder couponmonk_project
is the new one. 内部文件夹couponmonk_project
是您当前的项目,而外部文件夹couponmonk_project
是新项目。 Thus, file /home/giri/couponmonk_project/__init__.py
should be something like: 因此,文件/home/giri/couponmonk_project/__init__.py
应该类似于:
from couponmonk_project import app
import couponmonk_project.views
app.run()
and your file /home/giri/couponmonk_project/coupon_monk/project__init__.py
is: 并且您的文件/home/giri/couponmonk_project/coupon_monk/project__init__.py
是:
from flask import Flask
app = Flask(__name__)
app.debug = True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.