我在 docker 中有一个 python Flask 脚本。该脚本以这种方式从 docker-compose 获取一些环境变量:
app = Flask(__name__)
DEFAULT_PSW = os.environ['DEFAULT_PSW']
@app.route("/")
def manager():
...
我想使用 Sphinx 来记录它。我能够配置它,但是当我运行“make html”时,我得到这个异常:
WARNING: autodoc: failed to import module 'main'; the following exception was raised:
Traceback (most recent call last):
File "/home/lorenzo/.local/lib/python3.7/site-packages/sphinx/ext/autodoc/importer.py", line 32, in import_module
return importlib.import_module(modname)
File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/lorenzo/Desktop/Dev/project/script/manager_flask/manager/manager/main.py", line 20, in <module>
DEFAULT_PSW = os.environ['DEFAULT_PSW']
File "/usr/lib/python3.7/os.py", line 679, in __getitem__
raise KeyError(key) from None
KeyError: 'DEFAULT_PSW'
如果我很好地理解了 sphinx 的工作原理,那么问题是 sphinx 会尝试“编译”我的脚本,当然,在此过程中它不会找到环境变量。我能做什么?
编辑: 我为收到此错误所做的步骤:
出于文档目的,在 conf.py 文件中设置并导出密钥:
os.environ("DEFAULT_PSW", "default_psw")
您必须在conf.py中设置环境变量,如下所示: os.environ["DEFAULT_PSW"] = "some_value"