用__ In __ In__。py

问题描述 投票:0回答:1
我试图摆弄“来自。导入mydb”和“来自。进口 *”的一些变体,但没有任何运气。

代码并不复杂,请参见下文。 WSGI加载一个简单的烧瓶应用程序,然后尝试加载模块并在模块中访问单个变量,但我似乎无法使其工作。我觉得我错过了一些明显的东西...

/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/hellopy/
init

.py

__all__ = ["mydb"]

/var/www/html/hellopy/mydb.py dbkey = "abc123"

错误:

[THU 3月13日17:15:47.192392 2025] [WSGI:错误] [PID 5735:TID 5841] [远程127.0.0.1:37416] mod_wsgi(pid = 5735):无法执行python 脚本文件'/var/www/html/hellopy/hellopy.wsgi'。 [3月13日 17:15:47.192441 2025] [WSGI:错误] [PID 5735:TID 5841] [远程 127.0.0.1:37416] mod_wsgi(pid = 5735):发生了例外,处理了wsgi脚本'/var/www/html/hellopy/hellopy/hellopy.wsgi'。 [3月13日 17:15:47.192611 2025] [WSGI:错误] [PID 5735:TID 5841] [远程 127.0.0.1:37416]追溯(最近的最新电话):[THU 3月13日17:15:47.192706 2025] [WSGI:错误:错误] [PID 5735:TID 5841] [远程 127.0.0.1:37416]文件“/var/www/html/hellopy/hellopy.wsgi”,第8行,在[THU MAR 13 17:15:47.192726 2025] [wsgi] [wsgi:wsgi:error] 5735:TID 5841] [远程127.0.0.1:37416] mydbkey = config.dbkey [THU 3月13日17:15:47.192732 2025] [WSGI:错误] [PID 5735:TID 5841] [远程127.0.0.1:37416] ^^^^^^^^^^^^ [THU MAR 13 17:15:47.192747 2025] [WSGI:错误] [PID 5735:TID 5841] [远程 127.0.0.1:37416] attributeError:模块'app.config'没有属性'dbkey'

    

app.config

在这里应该是此事的一个属性:
python wsgi python-module
1个回答
0
投票

您用

设置并检索条目
app.config['dbkey'] = 'abc123'

or

mydbkey = app.config['dbkey']

不使用
__init__.py

__all__
或您尝试过的任何其他东西。

从错误消息中,看起来您创建了一个名为

app
的软件包,该软件包用一个名为
config
的子模块(您都没有向我们展示),并且由于某种原因,该子模块具有

dbkey
属性。但是您的

__init__.py

用于
hellopy
软件包,而它所做的只是使其
from hellopy import *
进口
hellopy.mydb
subsodule。
it不会执行与您创建的
app.config
模块或您应该访问的
app.config
映射有关的任何事情。
	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.