Visibility of os.environ to C extension modules

筅森魡賤 提交于 2020-01-13 13:50:10

问题


If I change environment variable using os.environ, do the modules I import afterwards see that change?

Specifically, sqlite3 requires the use of an environment variable to determine its temporary file location. But if I use os.environ['SQLITE_TMPDIR'] = '.' before import sqlite3, it does not have the desired effect. Why?


回答1:


The sqlite3 module is just a wrapper for the SQLite C library, so it will not directly see any changes made to os.environ.

However, the documentation says:

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.

So if the SQLite library is initialized after you've changed os.environ, it will see the changes.

Please note that SQLite reads different environment variables on Unix-y OSes and on Windows.



来源:https://stackoverflow.com/questions/36732249/visibility-of-os-environ-to-c-extension-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!