os.environ对C扩展模块的可见性 [英] Visibility of os.environ to C extension modules
问题描述
如果我使用 os.environ
更改环境变量,那么以后导入的模块会看到这个变化吗?
具体来说, sqlite3
需要使用环境变量来确定其临时文件位置。但是如果在 import sqlite3
之前使用 os.environ ['SQLITE_TMPDIR'] ='。'
,则它没有期望的效果。为什么?
sqlite3
模块只是SQLite的一个包装C库,所以它不会直接看到对 os.environ
所做的任何更改。
但是,文档说:
如果平台支持 putenv()函数,这个映射可能用于修改环境以及查询环境。修改映射时将自动调用 putenv()
所以如果SQLite库在更改 os.environ $ c $请注意,SQLite读取不同的环境变量在Unix-y操作系统上和在Windows上。
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?
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.
这篇关于os.environ对C扩展模块的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!