英文:
Python project not importing a local module
问题
我有一个使用Flask的Python项目,我在一个名为forms的模块中设置了一个表单。我在Windows上测试我的应用程序,一切正常。然后我在Debian上更新我的项目,使用Apache2来运行项目。在那里,我遇到错误:ModuleNotFoundError: No module named 'forms'
我的项目组织结构如下:
flaskapp.wsgi
flask_app
__init__.py
forms.py
而__init__.py
以以下方式开始:
from flask import Flask, redirect, url_for, request, render_template, send_from_directory, abort
from requests import Request, Session
from forms import OrderForm
我使用以下代码检查了sys.path
:
import sys
print(sys.path)
在Python控制台中运行时,输出为:
['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/dist-packages', '/usr/lib/python3/dist-packages']
我不确定我需要做什么来将forms
用作模块。
英文:
I have a python project using Flask and I have a form I have setup in a module I called forms. I test my app in Windows and it works just fine. I then update my project on Debian where I use Apache2 to run the project. There I get the error: ModuleNotFoundError: No module named 'forms'
My project is organized like so:
flaskapp.wsgi
flask_app
__init__.py
forms.py
And __init__.py
starts with:
from flask import Flask, redirect, url_for, request, render_template, send_from_directory, abort
from requests import Request, Session
from forms import OrderForm
I checked the sys.path
with:
import sys
print(sys.path)
when using the python console and got:
['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/dist-packages', '/usr/lib/python3/dist-packages']
I'm not sure what I need to do to get it to use forms
as a module
答案1
得分: 1
"Try from .forms import OrderForm
. This is referencing to the same folder. Otherwise flask_app.forms should work too."
英文:
Try from .forms import OrderForm
.
This is referencing to the same folder. Otherwise flask_app.forms should work too.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论