简体   繁体   中英

Flask Hello World error

I installed Python, Flask following these articles in CentOS 6.6

https://github.com/h2oai/h2o/wiki/Installing-python-2.7-on-centos-6.3.-Follow-this-sequence-exactly-for-centos-machine-only

http://flask.pocoo.org/docs/0.10/installation/

http://flask.pocoo.org/

Now When I am running hello.py ie

    from flask import Flask
app = Flask(__name__)

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

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

I got this error

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

Is there anything that needs to be done to run Flask beside that is provided in those documentation?

Thanks

The code is correct, so probably you haven't installed Flask globally.

Have you installed through sudo apt-get install python-virtualenv or through easy_install ?

I had my hello.py in /opt/myproject/flask/hello.py I had my venv in /opt/myproject/flask/venv

In /opt/myproject/flask I was doing . venv/bin/activate . venv/bin/activate and running the python hello.py .

But once I went to /opt/myproject/flask/venv and did . bin/activate . bin/activate and came back to /opt/myproject/flask and ran python hello.py everything came out fine.

Obviously as mentioned above you haven't installed flask yet.

Essentially if you create too many projects, then use virtualenvwrapper where you can list your environments by executing the command:

workon

Access your env like this:

workon myenv

Think of any project to be encapsulated from other project by wrapping all the packages inside a virtual environment and this env needs to be activated before running the project. Any module/package you import in your project should be preinstalled inside your environment by using this example below:

pip install Flask
pip install SQLAlchemy

And you can list your packages inside your environment by executing this command:

pip list

Usually you should have all the packages added to your requirements.txt to keep track on things and make it easy to install from scratch by executing this command:

pip install -r requirements.txt

Follow these simple steps and you'll be on the safe side always!

Happy coding, J.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM