Forums

Trouble importing package installed in venv. Older version in default packages

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 just completely deleted my previous venv, and installed a new on. I reloaded my site. I opened that new venv from the web tab, where it says "Start a console in this virtualenv".

if I type

pip freeze
Flask-HTTPAuth==3.1.1

pip3.3 freeze ## appears to give me system packages rather than venv ones
Flask-HTTPAuth==2.2.0

I am still getting the same error.

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?

Hi there, it looks like your webapp and virtualenv are using Python2.7, so that's why, in the virtualenv, "pip" shows you the virtualenv packages, but "pip3.3" shows you the system-wide Python3.3 packages.

As to why the imports aren't working as the docs say they should, I'm not sure. Could there be a typo?

Sorry, I havent kept this updated as I pursued the matter.

This seems to be where I am at:

PythonAnywhere seems to be calling the old module over the new module for this specific package. Other modules installed the same way are working without hitch. If I run python from my virtual machine bash console, I am able to call the package without error.


Overview of what I've done to investigate:

My web app is using python 3.4. I had thought 3.3 was pips version number and had seen it recommended online for a similar issue.

I did

pip3.4 freeze

that displayed the same results as

pip freeze

Not really sure what the difference is, but I uninstalled everything using pip uninstall and that went smoothly.

pip uninstall flask_httpauth
#... success
pip3.4 install flask_httpauth

I then went and looked in my venv site packages to find the source file. Upon opening the file, I can see that I should be able to import HTTPTokenAuth from the package.

I went back to the bash council I opened to my virtual environment, and opened python. Python 3.4 loaded. I then called the module as shown below, and it worked without error.

from flask_httpauth import HTTPTokenAuth

During this process I completely deleted my previous virtual enviorment. I found that I was missing necessary modules that caused errors since they were now missing. I installed those in the same console using

pip3.4 install X

Which got everything that was working working again. This suggests to me that I am installing flask_httpauth the proper way, but something is wrong still.

The developer of the package commented on my stack overflow post suggesting I may be using an older version.

"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"

I am not using a shebang. I am using the area of the web tab dedicated to virtual enviorments.

I'm confused: You say that from flask_httpauth import HTTPTokenAuth worked without errors after your installed flask_httpauth into your virtualenv, but then you say "something is wrong still" - what's wrong?

It works in this scenario:

  1. Go to my web tab and click the link for "Start a console in this
    virtualenv" for the only virtualenv I have linked to this app.

  2. Enter the following into the bash console:

python (venv) 13:50 ~$ python Python 3.4.3 (default, Oct 14 2015, 20:28:29) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information.

from flask_httpauth import HTTPTokenAuth

It does not work in this scenario:

  1. Open open flask_app.py for my web app.
  2. Click save and run

Traceback (most recent call last): File "/home/ExperimentsWithCode/getmebro/flask_app.py", line 12, in <module> from flask_httpauth import HTTPTokenAuth ImportError: cannot import name HTTPTokenAuth

import flask_httpauth from flask_httpauth import HTTPTokenAuth Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: cannot import name HTTPTokenAuth

Oh. Ok. The issue there is that "save and run" does not run your code in a virtualenv unless you tell it to. You'll need to use a shebang for that - there are docs here.

Just out of interest, what are you trying to accomplish by using "Save and Run" on a Flask app?

Oh I see. I'm really sorry to trouble you with such nonsense.

Doing web based python is pretty new to me. I've been using the consoles inline with different py files in my app to call on functions to see what they return rather than saving, reloading the app, going to a webpage, getting an error, checking the error log, and looping through again.

Prior to that line, it always ran without error.

No problem. We're here to help with that sort of thing.

BTW: That's a really innovative and clever use of the "Save & Run" feature.

I just kinda of assumed that's what it's there for. How is it supposed to be used, if you dont mind me asking?

It's actually there for people to run code that is not related to web apps. So if they're running a calculation of some sort or some machine learning or something like that. That's not to say that your use is wrong in any way, it's just interesting.

Good to know. Thank you for all your help!