简体   繁体   English

ImportError:__init__.py文件中没有名为…的模块

[英]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. 我已经读到,追加到sysPYTHONPATH可能不是解决此问题的最佳方法。

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.

相关问题 ImportError:当包含___init__.py时,没有名为test的模块 - ImportError: No module named test when ___init__.py is included Python3错误“ImportError:没有名为的模块”有__init__.py - Python3 error “ImportError: No module named” have __init__.py ImportError:在__init__.py文件Python中导入类时没有名为''的模块 - ImportError: No module named ' ' while Import class in the __init__.py file Python ImportError:没有名为ex47.game的模块(是的,我有一个__init__.py文件*并且*我尝试了所有PYTHONPATH的东西 - ImportError: No module named ex47.game (and yes, I have an __init__.py file *AND* I tried all that PYTHONPATH stuff __init__.py文件是模块吗? - Is the __init__.py file a module? ImportError:没有名为…的模块(但是我有__init__.py文件并设置了PYTHONPATH) - ImportError: No module named … (but I have __init__.py files and have set PYTHONPATH) 文件“ /usr/lib/python2.7/importlib/__init__.py”,第37行,位于import_module __import __(name)ImportError中:没有名为django的模块 - File “/usr/lib/python2.7/importlib/__init__.py”, line 37, in import_module __import__(name) ImportError: No module named django ImportError __init__.py,烧瓶 - ImportError __init__.py, flask ImportError:我也制作了 __init__.py 文件 - ImportError: I have made __init__.py file as well 接收导入错误:没有名为***的模块,但有__init__.py - Receiving Import Error: No Module named ***, but has __init__.py
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM