Forums

Not able to import installed modules in virtualenv - python2.7

Hi, relatively new to python. I appear to have installed flask_utils_js and itsdangerous in my virtualenv using python 2.7.5, they appear with pip lost and pip freeze, but in the log file I get the errors

ImportError: No module named flask-util-js

When I commented the import lines out I then got the same with itsdangerous

ImportError: No module named itsdangerous

I see itsdangerous is already available here as standard for python 3.

Also, Is it possible for me to use python 2.7.3? I had problems before with a later version.

Cheers.

At a guess, you're not activating the virtualenv in your web app's WSGI file. Is that possible? There are instructions on this page on how to do that (the example is Django, but the change to activate the virtualenv is the same).

Re: Python 2.7.3, unfortunately it's not possible to use that on PythonAnywhere.

Hi, no I did that via virtualenvwrapper: workon vilfredo-core

That only affects the console you're in -- are you seeing these import errors in your web app logs? If so, then that's the problem. If they're in other logs, could you let me know which?

But actually, the problem is when trying to run in the browser. How does the wsgi file tie up with my virtual environment? Do I have to add something in that file so it can find the modules installed in the virtualenv? Currently I'm just including my project

path = '/home/ford/vilfredo-core'

So I'm guessing I need to add the directory where virtualenv stored my modules. That sound about right?

Hi, the error file is https://www.pythonanywhere.com/user/ford/files/var/log/ford.pythonanywhere.com.error.log

OK, so what you need to do is activate the virtualenv in your WSGI file. This page explains how to do that for Django, and Flask requires the same WSGI file changes -- search for "Now edit your wsgi file" in the page and you'll see what to do.

Ah, the lines

activate_this = '/home/virtualenvdemo/.virtualenvs/django16/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

OK it worked. :-) Just getting a 404 now.

The requested URL was not found on the server.

Being new to pyhton its a bit hard for me to see what lines here are purely django specific.

Yup, that all looks OK now. Now you just need to put the site together :-)

Hmm, well the site should already be together, in that the unit tests run and its the same code that runs from the flask test server. So I'm not sure what else I need to do. The request doesn't appear to be reaching the routes defined in the views file. But if you say everything has been set up fine then there must be something missing from the source files for interacting with an external server.

Edit: Ah, OK. Fixed it. Package setup seems a bit off so a few things to do, but at least the external server side seems properly wired up now and I have logging available.

Many thanks :-)

So it's working now? Excellent :-)

Well no, it's not working.

Either the server works and I get an import error when I try to import a variable defined in my init.py file

from . import db

though the same code works on my local machine, or I get a 404 The requested URL was not found on the server.

Completely baffled.