11353

Flask can't see modules installed by pip

I installed Flask on Python 2.7.12/Apache 2.4/Ubuntu 16.04.1 LTS. I've been using these two tutorials to get set up. I'm not running venv or any other virtual environment (unlike this post). Running a simple Flask app works fine (a la "hello world"). I have a <strong>flaskapp.wsgi</strong> and a <strong>flaskapp.py</strong> in the same directory (/var/www/html).

<strong>flaskapp.wsgi</strong>

import sys sys.path.insert(0, "/var/www/html/flaskapp") from flaskapp import app as application

<strong>flaskapp.py</strong>

from flask import Flask app = Flask(__name__) @app.route('/') def main(): return "hello world" if __name__ == '__main__': app.run()

But when I try to import a module outside of the basic set of Flask modules, eg. httplib2 (which I installed via pip), I get a 500 HTTP error. Simply adding import httplib2 to <strong>flaskapp.py</strong> throws this error.

Looking at the Apache error logs I see the following:

[wsgi:error] mod_wsgi (pid=22739): Target WSGI script '/var/www/html/flaskapp/flaskapp.wsgi' cannot be loaded as Python module. [wsgi:error] mod_wsgi (pid=22739): Exception occurred processing WSGI script '/var/www/html/flaskapp/flaskapp.wsgi'. [wsgi:error] Traceback (most recent call last): [wsgi:error] File "/var/www/html/flaskapp/flaskapp.wsgi", line 7, in <module> [wsgi:error] from flaskapp import app as application [wsgi:error] File "/var/www/html/flaskapp/flaskapp.py", line 7, in <module> [wsgi:error] import httplib2 [wsgi:error] ImportError: No module named httplib2

When I run python (not using Flask), I can import httplib2 without trouble. So it seems for some reason my Flask app can't access pip's imports.

Here's what I've done to troubleshoot so far:

I checked to see where httplib2 lives, using __file__:

>>> import httplib2 >>> httplib2.__file__ '/home/andrew/.local/lib/python2.7/site-packages/httplib2/__init__.pyc'

Then I checked both sys.path and pip.get_installed_distributions() from within the Flask app:

<strong>flaskapp.py</strong> (revised)

from flask import Flask, jsonify import pip, sys modules = "" for i in pip.get_installed_distributions(): modules = modules + " " + str(i) app = Flask(__name__) @app.route('/') def main(): return jsonify({ "modules":modules, "sys.path":sys.path }) if __name__ == '__main__': app.run()

output:

{ "available_modules": " Flask 0.12 Jinja2 2.9.5 itsdangerous 0.24 Werkzeug 0.11.15 MarkupSafe 0.23 click 6.7", "sys.path": [ "/usr/share/python-wheels/lockfile-0.12.2-py2.py3-none-any.whl", "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl", "/usr/share/python-wheels/retrying-1.3.3-py2.py3-none-any.whl", "/usr/share/python-wheels/requests-2.9.1-py2.py3-none-any.whl", "/usr/share/python-wheels/wheel-0.29.0-py2.py3-none-any.whl", "/usr/share/python-wheels/urllib3-1.13.1-py2.py3-none-any.whl", "/usr/share/python-wheels/ipaddress-0.0.0-py2.py3-none-any.whl", "/usr/share/python-wheels/colorama-0.3.7-py2.py3-none-any.whl", "/usr/share/python-wheels/distlib-0.2.2-py2.py3-none-any.whl", "/usr/share/python-wheels/pyparsing-2.0.3-py2.py3-none-any.whl", "/usr/share/python-wheels/packaging-16.6-py2.py3-none-any.whl", "/usr/share/python-wheels/html5lib-0.999-py2.py3-none-any.whl", "/usr/share/python-wheels/CacheControl-0.11.5-py2.py3-none-any.whl", "/usr/share/python-wheels/six-1.10.0-py2.py3-none-any.whl", "/usr/share/python-wheels/chardet-2.3.0-py2.py3-none-any.whl", "/usr/share/python-wheels/progress-1.2-py2.py3-none-any.whl", "/usr/share/python-wheels/setuptools-20.7.0-py2.py3-none-any.whl", "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl", "/var/www/html/flaskapp", "/usr/lib/python2.7", "/usr/lib/python2.7/plat-x86_64-linux-gnu", "/usr/lib/python2.7/lib-tk", "/usr/lib/python2.7/lib-old", "/usr/lib/python2.7/lib-dynload", "/usr/local/lib/python2.7/dist-packages", "/usr/lib/python2.7/dist-packages" ] }

So it can see the path where httplib2 lives, as well as the path to flask, but the available modules are only the Flask modules.

I'm not sure if this is a problem with my Apache configuration - I've consulted a number of how-tos and documentation, and this is the setup I've been using:

<strong>/etc/apache2/sites-enabled/000-default.conf</strong>

<VirtualHost *:80> WSGIDaemonProcess flaskapp threads=5 python-path=/var/www/html/flaskapp/flaskapp WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined <Directory /var/www/html/flaskapp> WSGIProcessGroup flaskapp WSGIApplicationGroup %{GLOBAL} Require all granted WSGIScriptReloading On </Directory> </VirtualHost>

I've looked through a number of SO posts, but most seem concerned with app not being visible to WSGI. Some posts seem close, but haven't solved my problem. I'd really appreciate any advice!

Note: I'm pretty comfortable with Python, familiar-ish with Flask, but pretty novice with Apache and WSGI. Detailed explanations/links for answers concerning the latter two topics (if appropriate) would really help.

Answer1:

You should add ~/.local/ to python-path on your Apache configuration.

WSGIDaemonProcess flaskapp threads=5 python-path=/var/www/html/flaskapp/flaskapp:/home/andrew/.local

You should also try to use virtual environments, and set python-home on WSGIDaemonProcess as the environment root.

Source: http://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIDaemonProcess.html

EDIT

You can also install httplib2 with sudo just like you installed the flask packages (as seen in one of your links)

Recommend

  • How to use google-closure-compiler-js for a node.js app without gulp/grunt/webpack?
  • Android Launcher application compilation on Eclipse
  • How to insert images into SQL Server database table
  • Can't Resolve fromJson
  • instance of class held by composition container in mef
  • Check for instance of Python multiprocessing.Connection?
  • How to fix error Cannot find name 'object' in angular 2 cli ckeditor
  • How to use Webpack loaders syntax ( imports/exports/expose) with ECMAScript 6 imports?
  • How to debug charts package 'No module named 'plot' ' Error
  • TableViewer shrinks to a single row with a scroll bar when new input is set
  • Destructuring assignment within import statements
  • How to Autowire repository interface from a different package using Spring Boot?
  • The android java class cannot recognize the R file
  • how to avoid the dependencies hell with unit test in angular 2+
  • Perl function name clash
  • Error while importing scikits.talkbox
  • JSF NumberFormatException with f:setPropertyActionListener
  • ImportError: cannot import name Pubnub
  • What does “t” refer to in this SQL?
  • CERN ROOT exporting data to plain text
  • Django simple Captcha “No module named fields” error
  • Why does access(2) check for real and not effective UID?
  • Using $this when not in object context
  • C# - Serializing and deserializing static member
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Sending data from AppleScript to FileMaker records
  • Perl system calls when running as another user using sudo
  • How to include full .NET prerequisite for Wix Burn installer
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Benchmarking RAM performance - UWP and C#
  • Is it possible to post an object from jquery to bottle.py?
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal