我试图弄明白为什么我会看到错误ModuleNotFoundError: No module named 'urlparse',但我从来没有在代码中调用urlparse.当我试图用pip安装urlparse时,我发现这个模块不存在.当我try 安装urllib时.用pip解析我在urllib中看到了相同的消息.作语法分析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()

推荐答案

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模块被重命名为urllib.用Python3进行解析.

这一点已在Github上的软件包维护人员那里提出.Github上的源代码看起来是固定的,但固定版本尚未推送到pypi.

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

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

Python-3.x相关问答推荐

如何将CSV或FDF数据解析到Python词典并注入到模板PDF表单中?

确定字符串的长度并提取前15或14个字符

Python多处理池:缺少一个进程

我无法直接在 VSCode 中运行该程序,但可以使用 VScode 中的终端运行它

Python webdrivermanager 和 Chrome 115.0 的 URL https://chromedriver.storage.googleapis.com/LATEST_RELEASE_115.0.5790 错误没有此类驱动程序

替换 .txt 文件中的项目列表

通过 Pandas 通过用户定义函数重命名数据框列

将水平堆叠的数据排列成垂直

根据按不同列中的值分组的平均值划分 DataFrame

我应该如何调整我的变量,以便如果有任何单词符合其中的条件,程序会将其附加到新列表中?

如何将搜索结果中的所有值保存在另一个列表中?

如何使用`re.findall`从字符串中提取数据

从 yahoo Finance python 一次下载多只股票

Pytorch:图像标签

简单的 get/post 请求在 python 3 中被阻止,但在 python 2 中没有

如何在 Python 中计算 cohen 的 d?

sys.stdin.readline() 读取时没有提示,返回 'nothing in between'

是否在未完成初始化的对象上调用了 del?

try 在 Windows 10 高 DPI 显示器上解决模糊的 tkinter 文本 + zoom ,但担心我的方法不是 Pythonic 或不安全

TypeError: write() 参数必须是 str,而不是字节(Python 3 vs Python 2)