[英]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.