我在Mac上的Python 3.7上运行代码。
由于某种原因,当我尝试运行“ run.py”文件时,它会打印出此错误:
Traceback (most recent call last):
File "/Users/sxxx/Desktop/python-project/aimupapp/routes.py", line 2, in <module>
from aimupapp import app
ModuleNotFoundError: No module named 'aimupapp'
[Finished in 0.7s with exit code 1]
[shell_cmd: python -u "/Users/sxxx/Desktop/python-project/aimupapp/routes.py"]
[dir: /Users/sxxx/Desktop/python-project/aimupapp]
[path: /Library/Frameworks/Python.framework/Versions/3.7/bin:/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]
这是我的文件夹结构:
├── aimupapp
│ ├── __init__.py
│ ├── __pycache__
│ ├── forms.py
│ ├── models.py
│ ├── routes.py
│ ├── site.db
│ ├── static
│ │ ├── bootstrap
│ │ ├── css
│ │ ├── img
│ │ └── main.css
│ └── templates
│ ├── index.html
│ ├── layout.html
│ ├── tutor.html
│ ├── ..
└── run.py
run.py
from aimupapp import app
if __name__ == '__main__':
app.run(debug=True)
__init__.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
app = Flask(__name__)
app.config['SECRET_KEY'] = 'xxxx'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
from aimupapp import routes
models.py
from aimupapp import db
routes.py
from flask import render_template, url_for, flash, redirect
from aimupapp import app
from aimupapp.forms import RegistrationForm, LoginForm
from aimupapp.models import User, Tutor
答案 0 :(得分:0)
对于Flask,我已经看到设置FLASK_ENV可以正确解决导入错误问题。
export FLASK_ENV={name of the folder containing application.py}
为我修复了