[Python] Executing & accessing python module code with __all__ in __init__.py

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    So I'm trying to write a simple wsgi python module where I use __init__.py to bring in variables & functions from another file but I can't seem to get the module to load. I keep getting the "AttributeError"

    I've tried fiddling with some variations of "from . import mydb" and "from . import *" but haven't had any luck.

    The code isn't anything complicated, see below. A wsgi loads a simple Flask app and then tries to load a module and access a single variable in the module, but I can't seem to get it to work. I feel like I'm missing something blindingly obvious...

    Edit: As user2357112 correctly pointed out I'm trying to run a sub package from my app directory (/var/www/html/hellopy/app/config/). I wrote out the paths but managed to get them wrong.

    Still fiddling with it trying to figure it out...

    /var/www/html/hellopy/hellopy.wsgi:

    import sys
    import logging

    from hellopy import app as application
    from app import config
    mydbkey = config.dbkey

    # Optional: Set logging
    logging.basicConfig(stream=sys.stderr)
    sys.stderr = sys.stdout


    /var/www/html/hellopy/hellopy.py:

    import sys
    from flask import Flask

    sys.path.insert(0, '/var/www/html/hellopy')

    app = Flask(__name__)

    #to restart apache, sudo systemctl restart httpd.service

    @app.route('/')
    def hello_world():
    return 'Hello, World! Py found! Updated right now!, '

    if __name__ == "__main__":
    application.run()


    /var/www/html/hellopy/app/config/init.py

    __all__ = ["mydb"]


    /var/www/html/hellopy/app/config/mydb.py

    dbkey = "abc123"


    The error:


    [Thu Mar 13 17:15:47.192392 2025] [wsgi:error] [pid 5735:tid 5841] [remote 127.0.0.1:37416] mod_wsgi (pid=5735): Failed to exec Python script file '/var/www/html/hellopy/hellopy.wsgi'. [Thu Mar 13 17:15:47.192441 2025] [wsgi:error] [pid 5735:tid 5841] [remote 127.0.0.1:37416] mod_wsgi (pid=5735): Exception occurred processing WSGI script '/var/www/html/hellopy/hellopy.wsgi'. [Thu Mar 13 17:15:47.192611 2025] [wsgi:error] [pid 5735:tid 5841] [remote 127.0.0.1:37416] Traceback (most recent call last): [Thu Mar 13 17:15:47.192706 2025] [wsgi:error] [pid 5735:tid 5841] [remote 127.0.0.1:37416] File "/var/www/html/hellopy/hellopy.wsgi", line 8, in [Thu Mar 13 17:15:47.192726 2025] [wsgi:error] [pid 5735:tid 5841] [remote 127.0.0.1:37416] mydbkey = config.dbkey [Thu Mar 13 17:15:47.192732 2025] [wsgi:error] [pid 5735:tid 5841] [remote 127.0.0.1:37416] ^^^^^^^^^^^^ [Thu Mar 13 17:15:47.192747 2025] [wsgi:error] [pid 5735:tid 5841] [remote 127.0.0.1:37416] AttributeError: module 'app.config' has no attribute 'dbkey'
    ​

    EDIT2:

    Thanks to user2357112 I figured out this works:

    Edit to /var/www/html/hellopy/hellopy.py:

    #!/var/www/html/hellowpy/venv/bin/python
    import sys
    import logging
    logger = logging.getLogger(__name__)
    from hellopy import app as application
    from app import config
    mydbkey = config.mydb.dbkey
    # Optional: Set logging
    logging.basicConfig(stream=sys.stderr)
    sys.stderr = sys.stdout
    logger.error("Logged message from hellopy " + mydbkey)


    Edit to /var/www/html/hellopy/app/config/init.py:

    # -*- coding: utf-8 -*-
    from . import mydb
    __all__ = ["mydb"]


    But it's not quite what I wanted. I wanted the dbkey object to be part of config object without having to dump it all into the init.py file.

    I know that's the "right" way to do it, that I shouldn't just put a ton of stuff in init.py but I haven't quite figured out how to do it :)

    Definitely getting closer :)

    Continue reading...

Compartilhe esta Página