2

I am trying to use token authentication for a Flask project.

from flask_httpauth import HTTPBasicAuth # works
from flask_httpauth import HTTPTokenAuth # does not work.

I get the following error

ImportError: cannot import name HTTPTokenAuth

I tried

pip install flask_httpauth --upgrade

But it claims everything is up to date. (Flask-HTTPAuth==3.1.1)

The docs suggest this is the proper way to import it, but for some reason it is not working. Any ideas how I can get the token auth to import?

Edit:: I deleted and recreated the virtual environment I was using.

I am using python anywhere.

The problem persists. I discovered an older version of Flask_httpauth is loaded by default (v2.2.0 instead of v3.1.1). I went into the site packages and saw the HTTPTokenAuth is there and should be called.

I tried doing

import flask_httpauth
print (flask_httpauth.__version__)

to check the version being called by my app, but that doesnt work for all python packages, and it seems flask_httpauth doesnt have that functionality.

There are no errors displayed where I have the virtual enviorment linked on the web tab of pythonanywhere.

ExperimentsWithCode
  • 1,174
  • 4
  • 14
  • 30
  • 1
    Not sure if this helps, but it works for me with a clean virtualenv using python 3.5.1. Is it possible you have some other conflicting package installed? Or your own module with a conflicting name perhaps? – Matt Healy Apr 05 '16 at 10:36
  • 2
    Hard to know why, but my guess is that you are installing the upgrade on one virtualenv, then using another when you run your application. Do you have a shebang line on your start up Python script? Check which Python interpreter you have in there, make sure it goes to the right virtualenv. – Miguel Grinberg Apr 05 '16 at 16:52
  • @Migeul I dont have a shebang line, but I am running it on PythonAnywhere, where that line is in another spot. I have it on my sites console page, and installed it by opening a bash shell directly in the virtual machine linked to the site on the console page. I suppose the best thing to do in to delete this virtual enviorment and create a new one that hopefully installs properly. Thank you for your help. – ExperimentsWithCode Apr 05 '16 at 18:13
  • Completely deleted my previous venv and installed a new one. I noticed the system has Flask-HTTPAuth==2.2.0 whereas the 3.1.1 version is installed on my venv. The problem persists, so I am assuming this is an issue with python anywhere, and have reposted my question on their forums. Hopefully they will be able to help. – ExperimentsWithCode Apr 05 '16 at 19:09

1 Answers1

3

PythonAnywhere dev here, just reposting the solution that was discovered from @ExperimentsWithCode's forum post. The problem was happening when the code was being run from the editor on PythonAnywhere. This is separate from the configuration that is done on the "Web" tab where the virtualenv was specified: people can run any code they want from the editor, regardless of which web app it's associated with, or even code that's not associated with a web app.

So the solution was what @Miguel suggested: use a shebang.

Giles Thomas
  • 6,039
  • 2
  • 33
  • 51