Python项目未导入本地模块。

huangapple go评论103阅读模式
英文:

Python project not importing a local module

问题

我有一个使用Flask的Python项目,我在一个名为forms的模块中设置了一个表单。我在Windows上测试我的应用程序,一切正常。然后我在Debian上更新我的项目,使用Apache2来运行项目。在那里,我遇到错误:ModuleNotFoundError: No module named 'forms'

我的项目组织结构如下:

  1. flaskapp.wsgi
  2. flask_app
  3. __init__.py
  4. forms.py

__init__.py以以下方式开始:

  1. from flask import Flask, redirect, url_for, request, render_template, send_from_directory, abort
  2. from requests import Request, Session
  3. from forms import OrderForm

我使用以下代码检查了sys.path

  1. import sys
  2. print(sys.path)

在Python控制台中运行时,输出为:

  1. ['', '/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:

  1. flaskapp.wsgi
  2. flask_app
  3. __init__.py
  4. forms.py

And __init__.py starts with:

  1. from flask import Flask, redirect, url_for, request, render_template, send_from_directory, abort
  2. from requests import Request, Session
  3. from forms import OrderForm

I checked the sys.path with:

  1. import sys
  2. print(sys.path)

when using the python console and got:

  1. ['', '/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.

huangapple
  • 本文由 发表于 2023年5月21日 02:21:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定