[PYTHON] ModuleNotFoundError: No module named'***' when running Flask

problem###

I decided to put a web server in an existing project in Pycharm. However, I put the flask project as a subpackage in the existing project.

Then, when I cd to the subpackage, started the falsk server, and accessed the routing, I got the following error:

flask.cli.NoAppException
lask.cli.NoAppException: While importing "Machine_Learning_Project.resnet.server.flaskr", an ImportError was raised:

Traceback (most recent call last):
  File "/home/user/anaconda3/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
    __import__(module_name)
  File "/home/user/Source/Machine_Learning_Project/resnet/server/flaskr/__init__.py", line 4, in <module>
    from resnet.server.flaskr.index import run_process_result, index_router
ModuleNotFoundError: No module named 'resnet'

In the editor pycharm, there are no errors in the import statement, but when I access the flask server, I get a gill on the web side that can't find the dependent packages.

Cause

I ran the squid command when I started the flask server.

cd /home/user/Source/Machine_Learning_Project/resnet/server
flask run

The path to the directory where you run this command is the root parsing of the flask server. Root path: / home / user / Source / Machine_Learning_Project / resnet / server The flask server only knows this path, so when you get to the imported package, you can only get to it from this path. I'm getting a package not found error because the imported package is in another directory

スクリーンショット 2020-07-05 14.13.01.png As you can see, the orange part is the root perspective of the flask server. Of course, when importing a package other than the orange part (red part), an error that cannot be found occurs. スクリーンショット 2020-07-05 14.18.44.png As you can see, the editor specifies the red part as the root path, so no error was displayed for the import part.

Solution

Just tell the falsk server the location of the package you want to import and it will be resolved. The method of teaching is the following code.

import sys
sys.path.append("/home/user/Source/Machine_Learning_Project")

Note that it should be placed before the import statement for the package you want to import.

Recommended Posts

ModuleNotFoundError: No module named'***' when running Flask
When lambda-uploader says No module named xxxx.
Today's python error: ModuleNotFoundError: No module named
ModuleNotFoundError in Python: No module named story
ModuleNotFoundError: No module named'py4j'
ModuleNotFoundError: No module named'flask_sqlalchemy'
[Python] ModuleNotFoundError: No module named'urlparse'
Resolve No module named tkinter
Python error: ModuleNotFoundError: No module named'flask'
'lambda_function': No module named When'selenium' appears
ModuleNotFoundError in poetry: No module named'setuptools'
I got "ModuleNotFoundError: No module named'azure'" when running a program using Azure SDK for Python
How to resolve ModuleNotFoundError: No module named XXX in Jupyter Notebook
What to do when ModuleNotFoundError: No module named'XXX' occurs in Python
Error when installing opencv-python on jetson nano (ModuleNotFoundError: No module named'skbuild')
Suddenly with Python PyInstaller No module named pyinstaller
Microsoft Cognitive Toolkit: ModuleNotFoundError: No module named'cntk._cntk_py'
Today's python error: ModuleNotFoundError: No module named'requests'
Today's python error: ModuleNotFoundError: No module named'bs4'
No module named'_ctypes' occurs when installing pipenv
python> import seiral> ImportError: No module named serial
ModuleNotFoundError: No module named'_bz2' error in pyenv Python
Import cv2 ModuleNotFoundError: No module named'cv2' in python3
What to do if "amazon-linux-extras" → "No module named amazon_linux_extras"
No module named What to do if you get'libs.resources'
ModuleNotFoundError: No module What to do if you get'tensorflow.contrib'
After installing pygame with conda install No module named font
ImportError: No module What to do when you are told