在不修改第三方代码的情况下抑制警告 [英] Suppress warning without modifying third party code

查看:22
本文介绍了在不修改第三方代码的情况下抑制警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用第三方开源工具并生成警告:

DeprecationWarning:编译器包在 Python 3.x 中已弃用并删除.

如何在不修改第三方代码的情况下抑制此警告?

我知道如何使用 warnings.filter() 但我不能使用它:我从命令行调用 py.test,因此没有单一的源代码我的代码行在警告被创建之前被执行.

围绕控制台脚本创建包装器不是一种选择.

解决方案

您可以添加一个 usercustomizesitecustomize 模块来调用 warnings.filter().它将在解释器启动时加载.

请参阅自定义模块:

<块引用>

Python 提供了两个钩子让您自定义它:sitecustomizeusercustomize.要了解它是如何工作的,您首先需要找到您的用户 site-packages 目录的位置.启动 Python 并运行此代码:

<预><代码>>>>进口网站>>>site.getusersitepackages()'/home/user/.local/lib/python2.7/site-packages'

现在您可以在该目录中创建一个名为 usercustomize.py 的文件,然后把你想要的任何东西放进去.它会影响每次调用Python,除非它以 -s 选项启动以禁用自动导入.

sitecustomize 的工作方式相同,但通常由计算机管理员在全局 site-packages 目录中创建,并在 usercustomize 之前导入.请参阅 site 的文档模块了解更多详情.

您可以使用 PYTHONUSERBASE 环境变量,因此您可以在此处将 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

在上面的例子中,将 PYTHONUSERBASE 设置为 /foo/bar,Python 将加载 /foo/bar/lib/python/site-packages/usercustomize.py 如果存在.

在 Python virtualenv 中,使用基于 Python 2.6 之前的 site.py 的自定义 site.py 文件;此版本省略了 getusersitepackages() 函数.如果文件 lib/pythonX.X/no-global-site-packages.txt 存在,usercustomize 模块将被导入.您必须使用虚拟环境lib/python/site-packages 目录中的sitecustomize.py文件代替.

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.

Creating a wrapper around the console script is not an option.

解决方案

You can add a usercustomize or sitecustomize module that calls warnings.filter(). It'll be loaded as the interpreter starts.

See The Customization Modules:

Python provides two hooks to let you customize it: sitecustomize and usercustomize. To see how it works, you need first to find the location of your user site-packages directory. Start Python and run this code:

>>> 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. It will affect every invocation of Python, unless it is started with the -s option to disable the automatic import.

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 before usercustomize. See the documentation of the site module for more details.

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:

$ 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.

In a Python virtualenv a customised site.py file is used that is based on a site.py from before Python 2.6; this version omits the getusersitepackages() function. If the file lib/pythonX.X/no-global-site-packages.txt exists, the usercustomize module will not be imported. You'll have to use a sitecustomize.py file in the virtual env lib/python/site-packages directory instead.

这篇关于在不修改第三方代码的情况下抑制警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆