简体   繁体   English

无法将flask-marshmallow导入python3项目

[英]Can't import flask-marshmallow into python3 project

I have only python 3.8.5 installed.安装了 python 3.8.5。

I started diving into Python and Flask for the first time (I'm coming from the javascript side of things).我第一次开始深入研究 Python 和 Flask(我来自 javascript 方面)。 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.我能够运行pipenv install flask flask-sqlalchemy flask-marshmallow marshmallow-sqlalchemy并且它们似乎都安装得很好。 They all appear in the pipfile.lock.它们都出现在 pipfile.lock 中。 flask-marshmallow is version 0.13.0.烧瓶棉花糖是 0.13.0 版。

pipfile.lock pipfile.lock

When I started coding, I was able to import flask and flask_sqlalchemy with no issues.当我开始编码时,我能够毫无问题地导入flask和flask_sqlalchemy。 Intellisense even helped me out with them. Intellisense 甚至帮我解决了这些问题。 But from flask-marshmallow import Marshmallow didn't seem to work.但是from flask-marshmallow import Marshmallow似乎没有用。

When I ran python app.py I get the following error当我运行python app.py ,出现以下错误

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新语言的良好开端 xD 这是迄今为止的整个文件供参考,如果我删除棉花糖行,它开始时没有任何问题

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.请确保,因为您已经创建了pipenv ,您已经在 IDE 中正确设置了 Python 解释器路径。

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 .我正在使用 VS 代码,正在使用python3.8pipenv

Even though I have installed the packages using pip3 , I was facing import issues while running the code.即使我已经使用pip3安装了软件包,我在运行代码时也pip3导入问题。 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 . Python 解释器路径( Cntrl+Shift+P -> Select Interpreter )未设置为新创建的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.在我的情况下,我试图运行命令flask db init ,它会给出上面的错误。 I got it to work by running the command with sudo privileges sudo flask db init .我通过使用 sudo 权限sudo flask db init运行命令来让它工作。 I hope this can help someone else out in a similar situation!我希望这可以帮助其他遇到类似情况的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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