简体   繁体   English

错误:没有名为“__main__.forms”的模块; '__main__' 不是 Python3 中的包

[英]Error: No module named '__main__.forms'; '__main__' is not a package in Python3

I am building a web app using Flask in Python.我正在使用 Python 中的 Flask 构建一个 Web 应用程序。 I have my main file named: flaskblog.py and it has following code:我的主文件名为:flaskblog.py,它有以下代码:

from flask import Flask, render_template

app = Flask(__name__)

** # I have some code here****


if __name__ == '__main__':
     app.run()

above code worked fine and gave desired output, I have only one .py file as of here.上面的代码运行良好并提供了所需的输出,截至此处我只有一个 .py 文件。

Now I have created a new .py file named: forms.py which has classes RegistrationForm and LoginForm and I tried to import these classes into flaskblog.py file, code is below,现在我创建了一个名为 forms.py 的新 .py 文件,其中包含类 RegistrationForm 和 LoginForm,我尝试将这些类导入到flaskblog.py 文件中,代码如下,

from flask import Flask, render_template
from .forms import RegistrationForm, LoginForm

app = Flask(__name__)

    ** # I have some code here****


if __name__ == '__main__':
      app.run()

the above code is giving me error:上面的代码给了我错误:

ModuleNotFoundError: No module named ' main .forms'; ModuleNotFoundError: 没有名为“ main .forms”的模块; ' main ' is not a package ' main ' 不是一个包

I am new to promgramming and thinking some changes need to be done in if __name__ == '__main__': but not able to figure it out.我是编程新手,并认为需要在if __name__ == '__main__':进行一些更改if __name__ == '__main__':但无法弄清楚。 can anyone assist please?有人可以帮忙吗?

and welcome to SO.欢迎来到 SO。 First I would recommend reading this link: https://realpython.com/python-modules-packages/ since it explains how Python modules and packages are working.首先,我建议阅读此链接: https : //realpython.com/python-modules-packages/,因为它解释了 Python 模块和包的工作方式。

You should be able to solve the from if you change:如果您更改,您应该能够解决 from:

from .forms import RegistrationForm, LoginForm

to:到:

from forms import RegistrationForm, LoginForm

暂无
暂无

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

相关问题 Python pickle:ImportError:没有名为__main__的模块 - Python pickle: ImportError: No module named __main__ ModuleNotFoundError: 没有名为“__main__.gtts”的模块; '__main__' 不是一个包 - ModuleNotFoundError: No module named '__main__.gtts'; '__main__' is not a package ModuleNotFoundError:没有名为'__main __。models'的模块; '__main__'不是包 - ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package 在docker中运行龙卷风python:ModuleNotFoundError:没有名为'__main __。config'的模块; '__main__'不是一个包 - running tornado python in docker: ModuleNotFoundError: No module named '__main__.config'; '__main__' is not a package Python 3 - 在同一目录中导入.py文件 - ModuleNotFoundError:没有名为'__main __。char'的模块; '__main__'不是包 - Python 3 - importing .py file in same directory - ModuleNotFoundError: No module named '__main__.char'; '__main__' is not a package 我遇到一个错误“ ModuleNotFoundError:没有名为'__main __。models'的模块; “ __main__”不是软件包” - I'm having an error “ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package” Docker Python3:找不到'__main__'模块 - Docker python3: can't find '__main__' module 如何在 Python3 中使用 pyclbr 搜索 __main__ 模块? - How to search for a __main__ module using pyclbr in Python3? ImportError:没有名为__main__的模块(Python 2.7和Pickle) - ImportError: No module named __main__ (Python 2.7 & Pickle) ModuleNotFoundError:没有名为'__main __。web_app'的模块; 从子文件夹导入包时,'__ main__'不是包 - ModuleNotFoundError: No module named '__main__.web_app'; '__main__' is not a package while importing package from sub folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM