简体   繁体   English

Python flask-cors ImportError: No module named 'flask-cors' 树莓派

[英]Python flask-cors ImportError: No module named 'flask-cors' Raspberry pi

I'm following the flask-cors tutorial from the documentation here: https://pypi.python.org/pypi/Flask-Cors我正在关注此处文档中的 flask-cors 教程: https://pypi.python.org/pypi/Flask-Cors

but when i installed it on my raspberry pi and run my python app i'm getting this error但是当我将它安装在我的树莓派上并运行我的 python 应用程序时,我收到了这个错误

Traceback (most recent call last): File "app.py", line 3, in <module> from flask_cors import CORS, cross_origin ImportError: No module named 'flask_cors'

here is my python script:这是我的 python 脚本:

from flask import Flask
from Main import main
from flask_cors import CORS, cross_origin    
app = Flask(__name__)
CORS(app)
main = main() 

@app.route('/turn' ,methods=['GET', 'OPTIONS'])
def index():
  return main.turn()

if __name__ == '__main__': 
  app.run(debug=True, host='0.0.0.0')

它终于对我有用

pip install -U flask-cors

If you import sys and print(sys.path) , this will show you where your available packages are installed.如果您import sysprint(sys.path) ,这将显示您的可用软件包的安装位置。

In the event that pip installed flask_cors outside of one of these directories, you should move the file to one of the directories or you can sys.path.append(<your path to flask_cors>) .如果 pip 在这些目录之一之外安装了flask_cors ,您应该将文件移动到目录之一,或者您可以sys.path.append(<your path to flask_cors>)

To prevent pip from installing into a bad directory, I would recommend this answer为了防止 pip 安装到错误的目录中,我会推荐这个答案

I spent a day trying to resolve this.我花了一天时间试图解决这个问题。 Here are my steps: First i closed my VSCODE and open it again, then run这是我的步骤:首先我关闭了我的 VSCODE 并再次打开它,然后运行

pip uninstall flask
pip uninstall flask_cors
pip3 install flask
pipenv shell
pipenv install flask_cors
pip install pipreqs
git add .
git commit -am "New cor resolved"
git push heroku master

I had a similar issue where after installing flask_cors it was still giving me a ModuleNotFoundError.我有一个类似的问题,在安装 flask_cors 后,它仍然给我一个 ModuleNotFoundError。 Try this:试试这个:

sudo pip3 uninstall flask_cors
sudo pip3 install Flask-Cors

It's a subtle difference but it worked for me.这是一个微妙的区别,但它对我有用。 Even though flask_cors appears to install a package and it's written that way in python when actually importing it, the actual package name installed with pip is Flask-Cors.尽管flask_cors 似乎安装了一个包并且它在实际导入时是用python 编写的,但使用pip 安装的实际包名称是Flask-Cors。

如果您在 Conda env 中遇到此问题,请根据 conda 文档使用-

conda install -c conda-forge flask_cors

Following command working in my case.以下命令适用于我的情况。 Change python version according to your system.根据您的系统更改 python 版本。

python3.7 -m pip install flask

If you're using pipenv, you might just need to turn on your virtual environment too by typing:如果您正在使用 pipenv,您可能只需要通过键入以下内容来打开您的虚拟环境:

pipenv shell

while in your root directory (with the pipfile.lock file in it).在您的根目录中(其中包含 pipfile.lock 文件)。

When my virtual environment was running I installed flask-cors and I got the same error.当我的虚拟环境运行时,我安装了 flask-cors,但我得到了同样的错误。 Then I deactivated the virtual environment and run the command:然后我停用虚拟环境并运行命令:

pip install -U flask-cors

如果您以 sudo 身份运行 Python 脚本,那么...

sudo pip install -U flask-cors

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

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