admin管理员组

文章数量:1391976

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 :)

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 :)

Share Improve this question edited Mar 14 at 5:00 Jeremy Daniel Gregorio asked Mar 14 at 1:17 Jeremy Daniel GregorioJeremy Daniel Gregorio 1473 silver badges15 bronze badges 2
  • Were you expecting the app in from hellopy import app as application and from app import config to be the same app? That's not how Python imports work. – user2357112 Commented Mar 14 at 3:39
  • Yeah, I was completely off base, I did figure it out based on some ow what you wrote. Posting my edits :) – Jeremy Daniel Gregorio Commented Mar 15 at 2:18
Add a comment  | 

2 Answers 2

Reset to default 1

app.config is supposed to be an attribute on this thing here:

app = Flask(__name__)

You set and retrieve entries on it with

app.config['dbkey'] =  'abc123'

or

mydbkey = app.config['dbkey']

Not with __init__.py or __all__ or any of the other stuff you tried.

(You can also load configuration from environment variables, or data files, or other sources, as described in the docs.)


From the error message, it looks like you created a package named app with a submodule named config (neither of which you showed us), and expected that submodule to have a dbkey attribute for some reason. But your __init__.py is for the hellopy package, and all it does is make it so from hellopy import * imports the hellopy.mydb submodule.

It doesn't do anything related to the app.config module you created, or the app.config mapping you should have been accessing.

Thanks to user2357112 I figured it out, they key was I needed an _init_.py in the config folder in order to expose mydb's members later on, like so:

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

    #!/var/www/html/hellowpy/venv/bin/python

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

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

    # /var/www/myflaskapp/myapp.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/__init__.py

    from app.config import mydb
    config = mydb                   # this was the part I was missing, I need this to explose mydb.py's variables later on in my application.

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

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


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

    dbkey = "abc123"

本文标签: wsgiExecuting amp accessing python module code with all in initpyStack Overflow