1

When running my Flask application on my Ubuntu 16.04.3 x64 web server, I get the following error when trying to import the Flask-OpenID library v1.2.5:

 File "/usr/local/lib/python2.7/dist-packages/openid/oidutil.py", line 17, in <module>
 from urllib.parse import urlencode
 ImportError: No module named parse

I understand that this is happening because this module is getting loaded in using python 2.7.5 (which I have installed on my machine) instead of 3.5.2 (also have it installed).

Checking the version of python verifies that it is installed on the machine:

>> python -V
Python 3.5.2

The virtual environment was initialized with python 3:

python3 -m venv env

The Flask app still seems to be using files from the python 2.7.5.

If this is indeed the issue, is there any way to set a default or to make sure Flask uses the correct version of python? My app runs with no errors in my development environment on my PC.

Nafana
  • 41
  • 2
  • 6
  • 1
    Can you confirm pip version, if you installed without activating virtualenv, pip and pip3 selects python2.7 and 3. – Vipin Mohan Feb 16 '18 at 01:24
  • urllib.parse is a feature only available in python 3.x, so the Flask app is designed for 3.x, you should run the app under python 3.5 instead of python 2.7. – hcheung Feb 16 '18 at 01:30
  • Sure thing. Running pip -V shows me: pip 8.1.1 from /var/www/FlaskApp/FlaskApp/env/lib/python3.5/site-packages (python 3.5) I made sure to active the environment with "source env/bin/activate" – Nafana Feb 16 '18 at 01:32
  • Okay I think I fixed a part of the problem, my Apache web server was running a different version of mod_wsgi, which was compiled for python version 2.7 and lower. However with the brand new installation, I now get the error telling me that my own python files can't be imported. For example "ImportError: No module named 'api_utils'" – Nafana Feb 16 '18 at 02:18

1 Answers1

3

It turns out that my virtual environment was configured correctly, however the issue was in fact that mod_wsgi running on Apache Web server, was compiled across Python version 2.7.X and loaded up Flask with python 2.7.5 instead of 3.5.2.

To fix this issue I first updated to the version of wsgi that would run python 3.5.1+

sudo apt-get install libapache2-mod-wsgi-py3

From this point on, your flask application may not run since your virtual host, and wsgi file for your flask application needs to be configured properly with python version 3.

Follow this tutorial: http://devmartin.com/blog/2015/02/how-to-deploy-a-python3-wsgi-application-with-apache2-and-debian/

You should also change your VirtualHost configuration to the one shown and explained at this post: https://stackoverflow.com/a/39419825/9367575

Nafana
  • 41
  • 2
  • 6