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

mni*_*key 7 urlparse flask python-3.x

我试图弄清楚为什么我看到一个错误,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')
Run Code Online (Sandbox Code Playgroud)

if __name__ == '__main__':
    app.run()
Run Code Online (Sandbox Code Playgroud)

小智 19

对于python3,我使用过

from urllib.parse import urlparse

而不是from urlparse import parse_qsl, urlparse它的工作原理

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

  • 这实际上是关于flask_oauth的问题,而不是urlparse的问题。除非有人要重写库源代码(而不是像其他答案所建议的那样从 git 安装),否则这个答案不会有帮助。 (2认同)

Mat*_*aly 13

flask_oauth库不支持Python3 - 您将从traceback中看到:

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'
Run Code Online (Sandbox Code Playgroud)

在Python 3中更改了urlparse模块的行为:

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
Run Code Online (Sandbox Code Playgroud)