Pyhton "No module named" error with local imports

Asked

Viewed 112 times

0

I created a simple App using Flask, but I have a problem when importing the "Resources" that I created into another file.

This is the main file:

from flask_restful import Api
from flask import Flask

from flaskServer.resources.getSpidersNamesResource import SpidersNameResource
from flaskServer.resources.runSpiderResource import RunAllSpidersResource

app = Flask(__name__)
api = Api(app)

api.add_resource(RunAllSpidersResource, '/cnpjchecker/runAllSpiders')
api.add_resource(SpidersNameResource, '/cnpjchecker/getSpidersNames')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Error when rotating it:

Traceback (most recent call last):
  File "cnpjCheckerAPI.py", line 12, in <module>
    from flaskServer.resources.getSpidersNamesResource import SpidersNameResource
ModuleNotFoundError: No module named 'flaskServer'

It is important to also make clear that the folder 'flaskServer' presents a file init.empty py.

The project directory tree is organized as follows: inserir a descrição da imagem aqui

1 answer

0

When printing the directories that python is identifying, I discovered that it is not considering the project’s root directory but the 'flaskServer root' directory. I believe this is happening because I am running a file that is not in the root directory. When changing the Imports to:

from flask_restful import Api
from flask import Flask

from resources.getSpidersNamesResource import SpidersNameResource
from resources.runSpiderResource import RunAllSpidersResource

app = Flask(__name__)
api = Api(app)

api.add_resource(RunAllSpidersResource, '/cnpjchecker/runAllSpiders')
api.add_resource(SpidersNameResource, '/cnpjchecker/getSpidersNames')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Everything works out correctly. Unfortunately, pyCharm cannot identify that this import is correct and underlines the import lines. At the moment I will just ignore them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.