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.
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
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. As you can see, the editor specifies the red part as the root path, so no error was displayed for the import part.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