【问题标题】:No module named 'urlparse' but I'm not using urlparse没有名为“urlparse”的模块,但我没有使用 urlparse
【发布时间】:2018-05-03 03:31:34
【问题描述】:

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

【问题讨论】:

  • 可以发布整个回溯吗?可能您使用的某些软件包不支持 Python3。请检查此question

标签: python-3.x flask urlparse


【解决方案1】:

对于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】:

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

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

这已在Github 上与包维护人员提出。 Github上的源码貌似已经修复了,但是修复的版本还没有推送到pypi。

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

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

【讨论】:

  • flask-oauth 现在说它是无人维护的,而是使用 Flask-Dance
【解决方案3】:

在您的堆栈跟踪中,找到传播 ModuleNotFoundError 的文件。如果它来自您自己的源文件之一,请重构您的代码,因此 urlparse 的导入方式如下:

urllib.parse import urlparse

否则,查找该文件所属的模块。然后升级那个模块。

pip install --upgrade [module-name]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-13
    • 2018-01-13
    • 2013-01-02
    • 2017-04-06
    • 2016-05-04
    • 1970-01-01
    • 2016-09-08
    • 2011-07-25
    相关资源
    最近更新 更多