简体   繁体   中英

Can't import flask-marshmallow into python3 project

I have only python 3.8.5 installed.

I started diving into Python and Flask for the first time (I'm coming from the javascript side of things). I've already ran into an interesting issue in just my first few lines of code.

I was able to run pipenv install flask flask-sqlalchemy flask-marshmallow marshmallow-sqlalchemy and they all seemed to have installed just fine. They all appear in the pipfile.lock. flask-marshmallow is version 0.13.0.

pipfile.lock

When I started coding, I was able to import flask and flask_sqlalchemy with no issues. Intellisense even helped me out with them. But from flask-marshmallow import Marshmallow didn't seem to work.

When I ran python app.py I get the following error

Traceback (most recent call last):
  File "app.py", line 3, in <module>
    from flask_marshmallow import Marshmallow
ModuleNotFoundError: No module named 'flask_marshmallow'

I've tried uninstalling flask-marshmallow and marshmallow and reinstalling. The console always says the installation is successful but I can't seem to import it when all the other packages seem to work fine.

What a great start to a new language xD Here is the whole file so far for reference, if I remove the marshmallow line it starts without any issues

from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os

app = Flask(__name__)

if __name__ == "__main__":
    app.run(debug=True)

我建议检查站点包文件并确保它已安装在其中,如果没有,则将其安装在其他目录中 也尝试在另一个普通的 python 文件中使用它并检查它是否也有效 检查语法我知道我知道它显而易见,但只是说

Make sure that, because you have created a pipenv , you have set the Python interpreter path correctly in your IDE.

I was facing the exact issue (and that is how I reached this question). I am using VS code, was using python3.8 and pipenv .

Even though I have installed the packages using pip3 , I was facing import issues while running the code. After a futile search on the Internet, I realised that the issue was very silly.

The Python interpreter path ( Cntrl+Shift+P -> Select Interpreter ) was not set to the newly created pipenv . After I have set the interpreter path properly, the code resumed to function as expected.

In my case I was trying to run the command flask db init , and it would give the error above. I got it to work by running the command with sudo privileges sudo flask db init . I hope this can help someone else out in a similar situation!

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