0
votes

already searched database within stockoverflow -- there are no answers to this question!

"Import flask from Flask" stops working after deactivating env

  1. installed python3:
    • "python3 --version" -->returns Python 3.7.4
      1. installed virtualenv
    • "pip3 install virtualenv"
    • "virtualenv env"
    • "virtualenv --version" --> returns 16.7.4
      1. activated virtual environment
    • "source env/b/activate" --> creates (env) note at terminal prompt correctly
      1. installed flask in virtual environment
    • " pip3 install flask"
      1. In the python shell, import flask does not return an error.
      2. python program is:
        ...
        from flask import Flask
        app = Flask(__ name __)

@app.route('/')
def hello_world():
- return 'Hello, World!'
...

I get an error!
7. the error is: "unable to import flask"

ANY IDEAS ARE APPRECIATED. THANK YOU

I am using the following to run program:

export FLASK_APP=flask_blog.py

pip show flask --> returns: flask not found
pip3 show flask --> returns: flask version 1.1.1
pip doesn't find flask but pip3 does. What does that mean?

1
Possible that you are running the file using python2?Harshal Parekh
Which operating system is this on?v25

1 Answers

0
votes

virtualenv venv creates a virtual environment that uses Python 2. Since it isn't using Python 3, pip3 install flask uses the system pip3, which lives outside the virtual environment. This will install Flask outside of the virtual environment.

It's unclear how you're invoking Python. If you're typing python3, if you're getting the system python3, which will be able to import Flask.

Regardless, to correct this, remove venv, and build it again using

virtualenv --python=python3 venv

and then either activate the virtual environment, or use one of the wrappers that that virtual environment provides for you. e.g.,

venv/bin/pip install Flask

then

FLASK_APP=app.py venv/bin/flask run