programing tip

Flask ImportError : Flask라는 모듈이 없습니다.

itbloger 2020. 11. 10. 07:54
반응형

Flask ImportError : Flask라는 모듈이 없습니다.


여기 Flask 튜토리얼을 따르고 있습니다.

http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

./run.py를 시도하고 다음과 같은 결과를 얻습니다.

Traceback (most recent call last):
  File "./run.py", line 3, in <module>
    from app import app
  File "/Users/benjaminclayman/Desktop/microblog/app/__init__.py", line 1, in <module>
    from flask import Flask
ImportError: No module named flask

이것은 다음과 유사합니다.

ImportError : flask라는 모듈이 없습니다.

그러나 그들의 솔루션은 도움이되지 않습니다. 참고로, 내가 않는 한 사용자가 문제가 발생할 수 있습니다 언급라는 이름의 폴더 플라스크 있습니다.


만든 virtualenv를 삭제하십시오. 새로운 virtualenv 생성

virtualenv flask

그때

cd flask

virtualenv를 활성화합시다

source bin/activate

이제 명령 줄 왼쪽에 (flask)가 표시됩니다. 플라스크를 설치합시다

pip install flask

그런 다음 hello.py 파일을 만듭니다.

from flask import Flask
app = Flask(__name__)

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

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

그리고 그것을 실행

python hello.py

Flask 1.0.2 업데이트

새로운 플라스크 릴리스를 사용하면 스크립트에서 앱을 실행할 필요가 없습니다. hello.py는 이제 다음과 같이 보일 것입니다.

from flask import Flask
app = Flask(__name__)

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

그리고 그것을 실행

FLASK_APP=hello.py flask run

최신 명령을 실행할 때 hello.py가있는 폴더 안에 있어야합니다.

hello.py를 만들기 전의 모든 단계가이 경우에도 적용됩니다.


파이썬 3 사용

pip3 설치 플라스크


내가 해결할 수있는 유일한 방법은 사용자 python dir을 myapp.wsgi 파일에 추가하는 것입니다. 예로서:

sys.path.append('/home/deployer/anaconda3/lib/python3.5/site-packages')

전역 환경에 패키지를 설치하면 문제가 없을 것 같지만 사용자로 Python 패키지를 설치했습니다.


가상 환경을 활성화하고 Flask를 설치 한 후 app.py 파일을 생성했습니다. 나는 이것을 다음과 같이 실행한다 : python -m flask run. 이것이 도움이되기를 바랍니다!


나는 flasgger와 비슷한 문제가 있었다.

그 이유는 제가 항상

sudo pip install flask

하지만 어떤 이유로 항상 그렇게하는 것은 아닙니다. 때때로, 당신은 단지

pip install flask

또 다른 문제는 때때로 사람들 pip install Flask캡 F로 입력한다는 것입니다.

Posting this here in case somebody gets stuck. Let me know if it helped.

Useful Link: What is the difference between pip install and sudo pip install?


this is what worked for me,

sudo -H pip install flask

Or for pip3(python3) use :

sudo -H pip3 install flask

Sidenote

If you're using virtualenv it's a good idea to pip freeze >> requirements.txt to allow for the installed packages to be listed in one place. The sudo command and -H flag. For more on sudo's -H flag, look at Paul's answer. Hope this helps you.


I was using python2 but installed this: sudo apt-get install libapache2-mod-wsgi-py3

Instead of: sudo apt-get install libapache2-mod-wsgi

Correcting the installation solved the no flask problem.


  1. Edit /etc/apache2/sites-available/FlaskApp.conf
  2. Add the following two lines before the "WSGIScriptAlias" line:

WSGIDaemonProcess FlaskApp python-home=/var/www/FlaskApp/FlaskApp/venv/FlaskApp WSGIProcessGroup FlaskApp

  1. Restart Apache:service apache2 restart

I'm following the Flask tutorial too.And I met the same problem.I found this way to fix it.

http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world


In my case the solution was as simple as starting up my virtual environment like so:

$ venv/scripts/activate

It turns out I am still fresh to Python :)


Go to the flask file in microblog, then activate the virtual environment with source bin/activate, then go to flask/bin and install flask, and the rest of the packages, pip install flask. You will see flask listed inside bin directory. Try to run ./run.py again from microblog (or from wherever you have the file).


Even i too suggest u virtualenv, This might also solve ur problem.

sudo apt install python-flask

If u want to deploy in productionserver then go ahead with above solution else use virtualenv.


enter your python interactive mode then:

import sys

sys.path

it will print your path. Check wether flask is installed in the sys.path.

For MacOS, python path is under /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

But pip'll install python package by default under /Library/Python/2.7/site-packages

That's why it doesn't work for MacOS.


It worked for me after upgrading pip:

curl https://bootstrap.pypa.io/get-pip.py | python

Found that answer here: https://stackoverflow.com/a/49748494/3197202

Then I could just install flask:

pip install flask

This is what worked for me when I got a similar error in Windows; 1. Install virtualenv

pip install virtualenve
  1. Create a virtualenv

    virtualenv flask

  2. Navigate to Scripts and activate the virtualenv

    activate

  3. Install Flask

    python -m pip install flask

  4. Check if flask is installed

    python -m pip list


The flask script is nice to start a local development server, but you would have to restart it manually after each change to your code. That is not very nice and Flask can do better. If you enable debug support the server will reload itself on code changes, and it will also provide you with a helpful debugger if things go wrong. To enable debug mode you can export the FLASK_DEBUG environment variable before running the server: forexample your file is hello.py

$ export FLASK_APP=hello.py
$ export FLASK_DEBUG=1
$ flask run

in my case using Docker, my .env file was not copied, so the following env vars were not set:

.env.local: FLASK_APP=src/app.py

so in my Dockerfile i had to include:

FROM deploy as dev
COPY env ./env

which was referenced in docker-compose.yml

env_file: ./env/.env.local

another thing i had to pay attention to is the path variable to ensure my environment is used

ENV PATH $CONDA_DIR/envs/:my_environment_name_from_yml_file:/bin:$CONDA_DIR/bin:$PATH```

my answer just for any users that use Visual Studio Flesk Web project :

Just Right Click on "Python Environment" and Click to "Add Environment"

참고URL : https://stackoverflow.com/questions/31252791/flask-importerror-no-module-named-flask

반응형