如何确定模块名称以过滤特定的 Python 警告?
How to determine the module name to filter a specific Python warning?
使用 Python 可以 filter specific warnings 使用以下命令行语法:
-W action:message:category:module:line
但是如何确定特定警告的 module
的正确值?
考虑以下示例:
使用 (pipenv --python 3.6.5 install lxml==4.2.4
)
> python -W error -c "from lxml import etree"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "src/lxml/etree.pyx", line 75, in init lxml.etree
File "src/lxml/_elementpath.py", line 56, in init lxml._elementpath
ImportWarning: can't resolve package from __spec__ or __package__, falling back on __name__ and __path__
如果只想忽略特定的导入警告,如何找到要使用的模块名称? None 以下命令似乎是正确的。他们都仍然发出警告。
python -W error -W ignore::ImportWarning:lxml -c "from lxml import etree"
python -W error -W ignore::ImportWarning:lxml.etree -c "from lxml import etree"
python -W error -W ignore::ImportWarning:lxml._elementpath -c "from lxml import etree"
python -W error -W ignore::ImportWarning:etree -c "from lxml import etree"
python -W error -W ignore::ImportWarning:_elementpath -c "from lxml import etree"
python -W error -W 'ignore::ImportWarning:lxml[.*]' -c "from lxml import etree"
ImportWarning
的警告,其实是来自import.c,但需要用_frozen_importlib
过滤,警告信息中的栈不完整,省略了内部栈。您可以通过覆盖 warnings.showwarning
:
来获取此信息
import warnings
def showwarning(message, category, filename, lineno, file, line):
print(filename)
warnings.showwarning = showwarning
warnings.resetwarnings() # allow all warnings
from lxml import etree
您可以通过以下方式验证:
python -Werror::ImportWarning:_frozen_importlib -c 'import lxml.etree'
顺便说一句 ImportWarning
是 ignored by default.
使用 Python 可以 filter specific warnings 使用以下命令行语法:
-W action:message:category:module:line
但是如何确定特定警告的 module
的正确值?
考虑以下示例:
使用 (pipenv --python 3.6.5 install lxml==4.2.4
)
> python -W error -c "from lxml import etree"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "src/lxml/etree.pyx", line 75, in init lxml.etree
File "src/lxml/_elementpath.py", line 56, in init lxml._elementpath
ImportWarning: can't resolve package from __spec__ or __package__, falling back on __name__ and __path__
如果只想忽略特定的导入警告,如何找到要使用的模块名称? None 以下命令似乎是正确的。他们都仍然发出警告。
python -W error -W ignore::ImportWarning:lxml -c "from lxml import etree"
python -W error -W ignore::ImportWarning:lxml.etree -c "from lxml import etree"
python -W error -W ignore::ImportWarning:lxml._elementpath -c "from lxml import etree"
python -W error -W ignore::ImportWarning:etree -c "from lxml import etree"
python -W error -W ignore::ImportWarning:_elementpath -c "from lxml import etree"
python -W error -W 'ignore::ImportWarning:lxml[.*]' -c "from lxml import etree"
ImportWarning
的警告,其实是来自import.c,但需要用_frozen_importlib
过滤,警告信息中的栈不完整,省略了内部栈。您可以通过覆盖 warnings.showwarning
:
import warnings
def showwarning(message, category, filename, lineno, file, line):
print(filename)
warnings.showwarning = showwarning
warnings.resetwarnings() # allow all warnings
from lxml import etree
您可以通过以下方式验证:
python -Werror::ImportWarning:_frozen_importlib -c 'import lxml.etree'
顺便说一句 ImportWarning
是 ignored by default.