我试图找出为什么我看到一个错误ModuleNotFoundError: No module named 'urlparse',但我从来没有在我的代码中调用urlparse。当我尝试用pip安装urlparse时,我发现这个模块并不存在。当我尝试用pip安装urllib.parse时,我在urllib.parse上看到了同样的消息。No matching distribution found for urllib.parse。
这里我漏掉了什么?
from flask import Flask, request, redirect, url_for, session, g, flash, \
render_template
from flask_oauth import OAuth
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
# configuration
SECRET_KEY = 'development key'
DEBUG = True
# setup flask
app = Flask(__name__)
app.debug = DEBUG
app.secret_key = SECRET_KEY
oauth = OAuth()
# Use Twitter as example remote app
twitter = oauth.remote_app('twitter',
base_url='https://api.twitter.com/1/',
request_token_url='https://api.twitter.com/oauth/request_token',
access_token_url='https://api.twitter.com/oauth/access_token',
authorize_url='https://api.twitter.com/oauth/authenticate',
consumer_key='',
consumer_secret=''
)
@twitter.tokengetter
def get_twitter_token(token=None):
return session.get('twitter_token')
@app.route('/')
def index():
access_token = session.get('access_token')
if access_token is None:
return redirect(url_for('login'))
access_token = access_token[0]
return render_template('templates/index.html')
if __name__ == '__main__':
app.run()发布于 2018-05-03 11:47:07
flask_oauth库不支持Python3 -您将从回溯中看到:
Traceback (most recent call last):
File "app.py", line 3, in <module>
from flask_oauth import OAuth
File "/Users/matthealy/virtualenvs/test/lib/python3.6/site-packages/flask_oauth.py", line 13, in <module>
from urlparse import urljoin
ModuleNotFoundError: No module named 'urlparse'在Python 3中,urlparse模块的行为发生了变化:
https://docs.python.org/2/library/urlparse.html
在Python3中将urlparse模块重命名为urllib.parse。
这已经在Github上的包维护人员中提出了。Github上的源码看起来已经修复了,但是修复后的版本还没有被推送到pypi。
在Github上建议的解决方案是直接从源代码安装,而不是pypi:
pip install git+https://github.com/mitsuhiko/flask-oauth发布于 2019-07-31 13:15:02
对于python3,我使用了
from urllib.parse import urlparse
而不是from urlparse import parse_qsl, urlparse,而且它是有效的
发布于 2021-04-03 21:53:48
在堆栈跟踪中,找到传播ModuleNotFoundError的文件。如果它来自您自己的源文件,请重构您的代码,以便导入urlparse,如下所示:
urllib.parse import urlparse否则,查找该文件所属的模块。然后升级该模块。
pip install --upgrade [module-name]https://stackoverflow.com/questions/50146520
复制相似问题