I'm developing a REST API, using flask and flask-restful. I am currently having problems with flask-cli, since in the company environment I can not run with the application. What may be the problem?
In the enterprise, we use conda environments, for python 3 applications. Currently the environment for api consists of python 3.6.8 with flask 1.0.2 and flask-restful 0.37, running on a CentOS 7. Even recreating the environment by requirements or enviroment.yml of the conda, I could not run the application, different from my personal machine that is an ubuntu 18.04, with miniconda3 and env python 3.6.8, with the same flask packages and I can test without problems.
Error:
File "/home/user/opt/miniconda3/envs/comands/lib/python3.6/site-packages/flask/cli.py", line 235, in locate_app
__import__(module_name)
ModuleNotFoundError: No module named 'main'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/user/opt/miniconda3/envs/comands/lib/python3.6/site-packages/flask/cli.py", line 325, in __call__
self._flush_bg_loading_exception()
File "/home/user/opt/miniconda3/envs/comands/lib/python3.6/site-packages/flask/cli.py", line 313, in _flush_bg_loading_exception
reraise(*exc_info)
File "/home/user/opt/miniconda3/envs/comands/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/user/opt/miniconda3/envs/comands/lib/python3.6/site-packages/flask/cli.py", line 302, in _load_app
self._load_unlocked()
File "/home/user/opt/miniconda3/envs/comands/lib/python3.6/site-packages/flask/cli.py", line 317, in _load_unlocked
self._app = rv = self.loader()
File "/home/user/opt/miniconda3/envs/comands/lib/python3.6/site-packages/flask/cli.py", line 372, in load_app
app = locate_app(self, import_name, name)
File "/home/user/opt/miniconda3/envs/comands/lib/python3.6/site-packages/flask/cli.py", line 246, in locate_app
'Could not import "{name}".'.format(name=module_name)
flask.cli.NoAppException: Could not import "main".
Script Main:
from app import create_app, db
from flask_migrate import Migrate
app = create_app('development')
Migrate(app,db)
@app.shell_context_processor
def shell_context():
return dict(
app = app,
db=db
)
Script init.py:
from flask_restful import Api
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from config import config
db = SQLAlchemy()
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
api=Api(app, prefix="/api/v1")
db.init_app(app)
from app.resources.catcherDicts import Catcher
api.add_resource(Catcher, "/catcher")
return app
I wan to be able to run the tests and run the application in the company environment. Because even exporting the application path with export FLASK_APP = main.py
command flask run
cannot run the application.
The error:
ModuleNotFoundError: No module named 'main'
is telling you it can't find the main.py
module from your cli.py
module. This is happening because you set your entry point to your application as main.py
with this line of code: export FLASK_APP = main.py
. I suspect this environment variable is incorrect, I would try running your entry file directly and seeing if it works, if so your environment variable is incorrect. Additionally, ensure your <path>
is set appropriately if you're not running the file from the directory containing your entry point otherwise flask run
will not work.
Hopefully that helps!