0

I'm trying to learn flask. I have created a virtualenv and am using the hello.py example file as in here.

For reference, the contents of hello.py is as follows:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

I can run the file and it creates a functioning web application.

So now I've created a unit test file in the same directory as hello.py and it imports hello. The code for this file is as follows:

import os
import hello
import unittest

class FlaskrTestCase(unittest.TestCase):

    def setUp(self):
        self.app = hello.app.test_client()

if __name__ == '__main__':
    unittest.main()

When I run the unit test file it attempts to execute hello.py and fails with:

ImportError: No module named flask.

What am I missing?

7
  • Well, erm, did you install flask first? In the virtualenv you're using?
    – jonrsharpe
    Feb 16, 2015 at 22:11
  • 3
    @jonrsharpe: He says it works if he runs the hello.py directly it works, which suggests it's installed somehow.
    – BrenBarn
    Feb 16, 2015 at 22:13
  • As I said, the application in hello.py works. So I assume so, unless I'm missing even more than I thought.
    – David
    Feb 16, 2015 at 22:13
  • It's installed as per the installation instructions for flask, using a virtualenv rather than a global installation.
    – David
    Feb 16, 2015 at 22:14
  • Ah, sorry; and how are you running the unit test? Again, from inside the same virtualenv?
    – jonrsharpe
    Feb 16, 2015 at 22:21

1 Answer 1

2

It seems you have not activated your virtualenv yet, use the source virtualenv/bin/activate command to activate.

Relevant documentation: http://docs.python-guide.org/en/latest/dev/virtualenvs/

6
  • I've followed the installation instructions for flask and the application works in my browser (albeit it just returns "Hello world").
    – David
    Feb 16, 2015 at 22:15
  • 1
    In that case you probably are running the unit test outside of the virtualenv
    – Wolph
    Feb 16, 2015 at 22:20
  • That seems a reasonable supposition, but it's in the same directory as hello.py, which is also the same directory that contains the virtual env directory (venv).
    – David
    Feb 16, 2015 at 22:21
  • 1
    Being in a directory doesn't activate a virtualenv, you need to run source virtualenv/bin/activate command to activate the virtualenv. Here's the docs: docs.python-guide.org/en/latest/dev/virtualenvs
    – Wolph
    Feb 16, 2015 at 22:26
  • 1
    You are correct - I've worked it out. I had activated the virtualenv and started the application. This prevented me doing anything else in that terminal, so I fired up another terminal in the same location and tried to run the test class from there.
    – David
    Feb 16, 2015 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.