如果我使用 os.environ
更改环境变量,我之后导入的模块是否会看到该更改?
具体来说,sqlite3
requires使用环境变量来确定其临时文件位置。但是如果我在 import sqlite3
之前使用 os.environ['SQLITE_TMPDIR'] = '.'
,它没有达到预期的效果。为什么?
最佳答案
sqlite3
模块只是 SQLite C 库的包装器,因此它不会直接看到对 os.environ
所做的任何更改。
然而,documentation说:
If the platform supports the putenv() function, this mapping may be used to modify the environment as well as query the environment. putenv() will be called automatically when the mapping is modified.
因此,如果在您更改 os.environ
后初始化 SQLite 库,它将看到更改。
请注意,SQLite 读取不同的环境变量 on Unix-y OSes和 on Windows .
关于python - os.environ 对 C 扩展模块的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36732249/