内容
IDLEにて以下のFlaskのプログラムが正常に動作しません。
ちなみに以前は正常に動いていました。
Python
1from flask import Flask 2app = Flask(__name__) 3@app.route('/') 4def root(): 5 return 'test' 6if __name__ == '__main__': 7 app.run(debug=True, host='0.0.0.0', port=8080)
IDLEで実行すると以下のように表示されて、すぐに終了してしまいます。
IDLE_Shell
1 * Serving Flask app 'test' (lazy loading) 2 * Environment: production 3[31m WARNING: This is a development server. Do not use it in a production deployment.[0m 4[2m Use a production WSGI server instead.[0m 5 * Debug mode: on 6 * Restarting with stat
Python3.9のIDLEでは、
IDLE_Shell
1 * Serving Flask app 'test' (lazy loading) 2 * Environment: production 3[31m WARNING: This is a development server. Do not use it in a production deployment.[0m 4[2m Use a production WSGI server instead.[0m 5 * Debug mode: on 6 * Running on all addresses (0.0.0.0) 7 WARNING: This is a development server. Do not use it in a production deployment. 8 * Running on http://127.0.0.1:8080 9 * Running on http://192.168.10.123:8080 (Press CTRL+C to quit) 10 * Restarting with stat
のように正常に動作。
追加
コマンドプロンプトで実行すると以下のようになります。
C:\WINDOWS\system32>python Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from flask import Flask >>> app = Flask(__name__) >>> @app.route('/') ... def root(): ... return 'a' ... >>> app.run(debug=True, host='0.0.0.0', port=8080) * Serving Flask app '__main__' (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: on * Restarting with stat C:\Program Files\Python310\python.exe: can't find '__main__' module in 'C:\\WINDOWS\\system32' C:\WINDOWS\system32>
環境
- Windows10 21H2
- Python 3.10.2 => Python 3.10.4 Pythonを再インストールしました。
- Flask 2.1.2
- pip 21.2.4
試したこと
- 色々なサイトに記載されているコードの実行
- Flaskのアンインストール・インストール
- Pythonの再インストール
再インストールする際に
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. sacremoses 0.0.47 requires joblib, which is not installed.
と出てきました。
- ポート番号の変更
- app.run()の括弧内を空にしたらうまく動作しました。しかし、指定したポート番号で動作させたいです。
以下の御回答をもとに、いろいろ試してわかったこと
- Pyinstaller用にインストールしたあったPython3.9では正常に動作しました。
- debug=Trueを削除したら正常に動作しました。
結局debug=Trueを消して動かすことにしました。
コードの不具合などございましたらお願いします。
御回答宜しくお願い致します。
皆さん御回答有難うございました。
他にも解決策がありましたら詳しく教えていただけると嬉しいです。
当方の環境 (Windows10, Python 3.10.4, Flask 2.1.2) では問題なく実行できますので、ソースコードに間違いはないように思います。
IDLE ではなくコマンドプロンプトでもすぐに終了してしまいますか?
また、なにかエラーメッセージは出ていますか?

返信有難うございます。
コマンドプロンプトでの実行結果を内容に追加しておきましたので、ご確認いただけると幸いです。
宜しくお願い致します。
インターラクティブシェルで実行する必要があるのですか?
インターラクティブシェルで実行するのではなく、例えば "Test.py" などというソースファイルを作成して、それを実行すればいいかと思います。

返信有難うございます。
コマンドプロンプトにて「python test.py」のように実行したところうまくいきませんでしたが、
ファイルを移動して同じように実行したら何故かうまくいきました。
有難うございました。
回答1件
あなたの回答
tips
プレビュー