"No module named 'urlparse'" but I'm not using urlparse
Asked Answered
B

3

37

I'm trying to figure out why I am seeing an error

ModuleNotFoundError: No module named 'urlparse'

but I never call urlparse in my code.

When I try to install urlparse with pip, I am seeing that this module doesn't exist. When I try to install urllib.parse with pip I see the same message:

No matching distribution found for urllib.parse

What am I missing here?

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()
Braga answered 3/5, 2018 at 3:31 Comment(1)
Can post the whole traceback? May some package you use doesn't support Python3. Please check this questionProsperous
B
26

The flask_oauth library doesn't support Python3 - you'll see from the 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'

The urlparse module's behaviour was changed in Python 3:

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

The urlparse module is renamed to urllib.parse in Python 3.

This has been raised with the package maintainers on Github. The source on Github looks to be fixed, but the fixed version has not been pushed to pypi.

The solution suggested on Github is to install directly from source instead of pypi:

pip install git+https://github.com/mitsuhiko/flask-oauth
Barbie answered 3/5, 2018 at 3:47 Comment(2)
flask-oauth now says it is unmaintained and to use Flask-Dance insteadCottonade
Where do they mention using Flask-Dance instead?Spitfire
C
49

For python3 I have used

from urllib.parse import urlparse

instead of from urlparse import parse_qsl, urlparse and it works

recommended doc : https://docs.python.org/3/library/urllib.parse.html

Caponize answered 31/7, 2019 at 5:15 Comment(1)
This is actually a question about flask_oauth, not urlparse. This answer won't help unless somebody is going to rewrite the library source code (instead of installing from git, as the other answer suggests.)Masjid
B
26

The flask_oauth library doesn't support Python3 - you'll see from the 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'

The urlparse module's behaviour was changed in Python 3:

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

The urlparse module is renamed to urllib.parse in Python 3.

This has been raised with the package maintainers on Github. The source on Github looks to be fixed, but the fixed version has not been pushed to pypi.

The solution suggested on Github is to install directly from source instead of pypi:

pip install git+https://github.com/mitsuhiko/flask-oauth
Barbie answered 3/5, 2018 at 3:47 Comment(2)
flask-oauth now says it is unmaintained and to use Flask-Dance insteadCottonade
Where do they mention using Flask-Dance instead?Spitfire
E
1

In your stack trace, find which file propagates the ModuleNotFoundError. If it's from one of your own source files, refactor your code, so urlparse is imported like:

urllib.parse import urlparse

Otherwise, find the module to which that file belongs to. Then upgrade that module.

pip install --upgrade [module-name]
Eudemon answered 3/4, 2021 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.