没有名为“urlparse”的模块,但我没有使用 urlparse

新手上路,请多包涵

我试图弄清楚为什么我会看到错误 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()

原文由 mnickey 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2k
2 个回答

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'

urlparse 模块的行为在 Python 3 中发生了变化:

https://docs.python.org/2/library/urlparse.html

urlparse 模块在 Python 3 中重命名为 urllib.parse。

Github 上的包维护者已经提出了这个问题。 Github上的源貌似已经修复了,但是修复后的版本还没有推送到pypi。

Github上建议的解决方案是直接从源码安装而不是pypi:

 pip install git+https://github.com/mitsuhiko/flask-oauth

原文由 Matt Healy 发布,翻译遵循 CC BY-SA 4.0 许可协议

对于 python3 我用过

from urllib.parse import urlparse

而不是 from urlparse import parse_qsl, urlparse 它有效

推荐文档: https ://docs.python.org/3/library/urllib.parse.html

原文由 Ujjwal Karna 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进