Question

I'm having trouble starting a simple Python Flask app on my Raspberry Pi.

I have created a virtualenv with virtualenvwrapper and it seems to be working. When I run pip list I get the following output.

argparse (1.2.1)
Flask (0.10.1)
gunicorn (18.0)
itsdangerous (0.24)
Jinja2 (2.7.2)
MarkupSafe (0.21)
pip (1.5.4)
requests (2.2.1)
setuptools (2.2)
Werkzeug (0.9.4)
wsgiref (0.1.2)

As you can see

I have the following code in simple.py.

import os
from flask import Flask
from flask import request
import requests

app = Flask(__name__)

app.run(host='0.0.0.0', port=int("80"), debug=True)

@app.route('/hello')
def hello():
    return 'Hello world!'

When I run sudo python simple.py on the Raspberry Pi I get the following error.

Traceback (most recent call last):
  File "simple.py", line 2, in <module>
    from flask import Flask
ImportError: No module named flask

As far as I can see Flask is actually installed in my virtualenv.

Was it helpful?

Solution

When you're running it as sudo, the virtualenvs python and other variables are no longer used. You can easily prove this by running sudo pip freeze or sudo pip list and comparing the output with pip freeze or list respectively.

You have (at least) two options:

  1. Run without sudo
  2. Define which python you want to use. Virtualenvs are just wrappers for environments, so you can say sudo /path/to/virtualenv/bin/python simply.py and everything should work just fine.

I use the second one often (minus the sudo) when combining supervisord configurations with different python versions in virtualenvs, for instance when deploying flask applications. Works like a charm.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top