[英]Suppress warning without modifying third party code
We use a third party open source tool and it generates a warning: 我们使用第三方开源工具,它会生成一个警告:
DeprecationWarning: The compiler package is deprecated and removed in Python 3.x.
How can I suppress this warning without modifying the third party code? 如何在不修改第三方代码的情况下抑制此警告?
I know how to use warnings.filter()
but I can't use it: I call py.test
from the command line, thus no single source code line of my code gets executed before the warning gets created. 我知道如何使用warnings.filter()
但我无法使用它:我py.test
调用py.test
,因此在创建警告之前,我的代码没有单一的源代码行被执行。
Creating a wrapper around the console script is not an option. 不能选择在控制台脚本周围创建包装器。
You can add a usercustomize
or sitecustomize
module that calls warnings.filter()
. 您可以添加一个调用warnings.filter()
的usercustomize
或sitecustomize
模块。 It'll be loaded as the interpreter starts. 它将在解释器启动时加载。
See The Customization Modules : 请参阅自定义模块 :
Python provides two hooks to let you customize it:
sitecustomize
andusercustomize
. Python提供了两个钩子来让你自定义它:sitecustomize
和usercustomize
。 To see how it works, you need first to find the location of your user site-packages directory. 要查看其工作原理,首先需要找到用户site-packages目录的位置。 Start Python and run this code: 启动Python并运行以下代码:>>> import site >>> site.getusersitepackages() '/home/user/.local/lib/python2.7/site-packages'
Now you can create a file named
usercustomize.py
in that directory and put anything you want in it. 现在,您可以在该目录中创建名为usercustomize.py
的文件,并将所需内容放入其中。 It will affect every invocation of Python, unless it is started with the-s
option to disable the automatic import. 它会影响Python的每次调用,除非它以-s
选项启动以禁用自动导入。
sitecustomize
works in the same way, but is typically created by an administrator of the computer in the global site-packages directory, and is imported beforeusercustomize
.sitecustomize
以相同的方式工作,但通常由全局site-packages目录中的计算机管理员创建,并在usercustomize
之前usercustomize
。 See the documentation of thesite
module for more details. 有关更多详细信息,请参阅site
模块的文档。
You can influence where Python looks with the PYTHONUSERBASE
environment variable , so you can point Python to a per-project usercustomize.py
file here, provided you take into account the path lib/python/site-packages
is added to the base: 您可以使用PYTHONUSERBASE
环境变量影响Python的外观,因此您可以在此处将Python指向每个项目的usercustomize.py
文件,前提是您考虑了将lib/python/site-packages
添加到基础的路径:
$ python -m site --user-site
/Users/someuser/Library/Python/2.7/lib/python/site-packages
$ PYTHONUSERBASE=/foo/bar python -m site --user-site
/foo/bar/lib/python/site-packages
In the above example, with PYTHONUSERBASE
set to /foo/bar
, Python will load /foo/bar/lib/python/site-packages/usercustomize.py
if it exists. 在上面的示例中,将PYTHONUSERBASE
设置为/foo/bar
,Python将加载/foo/bar/lib/python/site-packages/usercustomize.py
如果存在)。
In a Python virtualenv a customised site.py
file is used that is based on a site.py
from before Python 2.6; 在Python virtualenv中,使用基于Python 2.6之前的site.py
的自定义site.py
文件; this version omits the getusersitepackages()
function. 此版本省略了getusersitepackages()
函数。 If the file lib/pythonX.X/no-global-site-packages.txt
exists, the usercustomize
module will not be imported. 如果存在lib/pythonX.X/no-global-site-packages.txt
,则不会导入usercustomize
模块。 You'll have to use a sitecustomize.py
file in the virtual env lib/python/site-packages
directory instead. 您必须在virtual env lib/python/site-packages
目录中使用sitecustomize.py
文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.