简体   繁体   English

在不修改第三方代码的情况下禁止警告

[英]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()usercustomizesitecustomize模块。 It'll be loaded as the interpreter starts. 它将在解释器启动时加载。

See The Customization Modules : 请参阅自定义模块

Python provides two hooks to let you customize it: sitecustomize and usercustomize . Python提供了两个钩子来让你自定义它: sitecustomizeusercustomize 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 before usercustomize . sitecustomize以相同的方式工作,但通常由全局site-packages目录中的计算机管理员创建,并在usercustomize之前usercustomize See the documentation of the site 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.

相关问题 如何使用 warnings.filterwarnings 抑制第三方警告 - How to suppress a third-party warning using warnings.filterwarnings 如何抑制 pytest 中的第三方日志 - How to suppress third party logs in pytest 抑制第三方服务不可用时引发的异常的最佳方法是什么? - Best way to suppress exceptions raised when third-party service is unavailable? 如何在不使用第三方模块的情况下使用python脚本从SVN签出代码时提供用户名和密码 - how to provide username and password while checking out the code from SVN using python script without third party module Django - 如何在不修改的情况下扩展第三方模型 - Django - how to extend 3rd party models without modifying 在VIM中执行Python代码时如何抑制警告 - How to suppress warning when executing Python code from within VIM matplotlib动画:写入没有第三方模块的png文件 - matplotlib animation: write to png files without third party module 如何在没有 python 的计算机上使用第三方库运行 python 脚本? - How to run python script with third party libraries on computer without python? 导入第三方 Python 项目而不将其添加到 sys.path - Import a Third Party Python Project without Adding It to sys.path 如何在没有第三方库的情况下使用python验证xml? - How to validate xml using python without third-party libs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM