简体   繁体   中英

Python Flask Import Error: No Module named xx

I run my code on Python 3.7 on a mac.

for some reason, when I try to run the "run.py" file it prints out this error:

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]

This is my folder structure:

├── 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

For Flask I have seen setting the FLASK_ENV correctly fixed the import error issue.

export FLASK_ENV={name of the folder containing application.py}

fixed it for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM