问题
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