0

I'm building a Flask application and try to install it afterwards in another venv. Issue is that my configuration file is not picked up.

I create the tar file with python setup.py sdist and end up with the following structure.

structure

Then I create a new venv and install the tar file with pip install energy-1.0.tar.gz When I then start a ptyhon shell and import app it returns ModuleNotFoundError: No module named 'config'

This is the code for my app file where I also tried to add the root directory to the path.


from flask import logging

os.path.abspath(os.path.dirname(__file__))

from app import app

if __name__ == "__main__":
    gunicorn_logger = logging.getLogger('gunicorn.error')
    app.logger.handlers = gunicorn_logger.handlers
    app.logger.setLevel(gunicorn_logger.level)
    app.run()

and the init file of the app module where the error occurs.

import os
from flask import Flask
from flask_login import LoginManager

from .auth.auth_controller import auth_app
from .database import db
from .database.models import User
from .profile.profile_controller import profile_app
from .uploads.file_upload_controller import file_upload_app

app = Flask(__name__)

gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)

app.config['SECRET_KEY'] = 'secret-key-goes-here'
env = app.config['ENV']

if env == "production":
    app.config.from_object('config.Config')
elif env == "development":
    app.config.from_object('config.ConfigDevelopment')
elif env == "local":
    os.environ['AUTHLIB_INSECURE_TRANSPORT'] = '1'
    app.config.from_object("config.ConfigLocal")

Stack:

>>> import app
Traceback (most recent call last):
  File "/private/tmp/tmp/lib/python3.9/site-packages/werkzeug/utils.py", line 873, in import_string
    module = __import__(module_name, globals(), locals(), [obj_name])
ModuleNotFoundError: No module named 'config'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/private/tmp/tmp/lib/python3.9/site-packages/app/__init__.py", line 35, in <module>
    app.config.from_object('config.Config')
  File "/private/tmp/tmp/lib/python3.9/site-packages/flask/config.py", line 161, in from_object
    obj = import_string(obj)
  File "/private/tmp/tmp/lib/python3.9/site-packages/werkzeug/utils.py", line 881, in import_string
    raise ImportStringError(import_name, e).with_traceback(sys.exc_info()[2])
  File "/private/tmp/tmp/lib/python3.9/site-packages/werkzeug/utils.py", line 873, in import_string
    module = __import__(module_name, globals(), locals(), [obj_name])
werkzeug.utils.ImportStringError: import_string() failed for 'config.Config'. Possible reasons are:

- missing __init__.py in a package;
- package or module path not included in sys.path;
- duplicated package or module name taking precedence in sys.path;
- missing module, class, function or variable;

Debugged import:

- 'config' not found.

Original exception:

ModuleNotFoundError: No module named 'config'

So the question is how to access the config from the root directory after installing the module with pip.

davidism
  • 121,510
  • 29
  • 395
  • 339
shalama
  • 1,133
  • 1
  • 11
  • 25

1 Answers1

0

Ok I figured it out with the help of this post pip install . creates only the dist-info not the package

I had to include the config as a py_modules in the setup.py file.

setup(
    ...
    py_modules=['config'],
   ...
)
shalama
  • 1,133
  • 1
  • 11
  • 25