简体   繁体   English

为什么在python中导入kivy时会打印信息行?

[英]Why do the info lines get printed when importing kivy in python?

When I import kivy in python: 当我在python中导入kivy时:

>>> import kivy

The following three debug lines get printed: 打印以下三个调试行:

[INFO   ] [Logger      ] Record Log in C:\Users\usrname\.kivy\logs\kivy_18-04-07_50.txt
[INFO   ] [Kivy        ] v1.10.0
[INFO   ] [Python      ] v3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]

I know this is rather intended than an error, but I'd like to suppress this output. 我知道这比错误更有意义,但是我想抑制这个输出。 I have read the file kivy\\__init__.py and figured out that the lines get printed when following line is executed: 我已经读取了文件kivy\\__init__.py并且发现在执行以下行时会打印这些行:

from kivy.compat import PY2

So I have read kivy\\compat.py too, which isn't even a big file (102 lines), but I still can't see why the debug lines get printed. 所以我也读过kivy\\compat.py ,这甚至都不是一个大文件(102行),但我仍然看不出为什么打印调试行。

I've executed every single line (including docstrings and comments) of the file in the python console and couldn't reproduce it. 我已经在python控制台中执行了文件的每一行 (包括文档字符串和注释),无法重现它。

Can somebody explain this to me? 有人可以向我解释一下吗? I assume that this is something internal and is only possible because I installed it with pip but actually I have no clue what's going on here. 我认为这是内部的,只有可能,因为我用pip安装它,但实际上我不知道这里发生了什么。

The output is generated by the kivy.logger module , which simply uses the standard Python logger module . 输出由kivy.logger模块生成,该模块只使用标准的Python logger模块

The module adds a logger to output to the console, unless the KIVY_NO_CONSOLELOG environment variable has been set: 除非已设置KIVY_NO_CONSOLELOG环境变量,否则模块会添加一个记录器输出到控制台:

if 'KIVY_NO_CONSOLELOG' not in os.environ:
    # ...
    formatter = ColoredFormatter(color_fmt, use_color=use_color)
    console = ConsoleHandler()
    console.setFormatter(formatter)
    Logger.addHandler(console)

From that point on, logged messages are printed out to your console. 从那时起,记录的消息将打印到您的控制台。

It is not the kivy.compat module that triggers all this; 触发所有这一切的不是kivy.compat模块; when importing a module inside a package, Python will always make sure that the package itself is loaded first. 在包中导入模块时,Python将始终确保首先加载包本身。 Thus import kivy.compat will trigger an import of kivy itself if not already loaded. 因此,如果尚未加载, import kivy.compat将触发kivy本身的导入。

It is kivy/__init__.py that outputs the first log message: 输出第一条日志消息是kivy/__init__.py

if RELEASE:
    Logger.info('Kivy: v%s' % (__version__))

but the kivy.logger.FileHandler object that is also configured (unless KIVY_NO_FILELOG is set in the environment) runs extra code when first used and inserts Logger.info('Logger: Record log in %s' % filename) before the Kivy: v<version> message is handled. 但是同样配置的kivy.logger.FileHandler对象(除非在环境中设置了KIVY_NO_FILELOG )在第一次使用时运行额外的代码并在Kivy: v<version>之前插入Logger.info('Logger: Record log in %s' % filename)处理Kivy: v<version>消息。 Finally, kivy/__init__.py executes Logger.info('Python: v{}'.format(sys.version)) to output the Python version info. 最后, kivy/__init__.py执行Logger.info('Python: v{}'.format(sys.version))以输出Python版本信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在使用python fire时总是打印类型信息? - Why do I always get type info printed when using python fire? 为什么在运行以下代码(python,Kivy)时出现“ TypeError”? - Why do we get the 'TypeError' when running the following code(python, Kivy)? Python 导入使用日志记录的 package 时,日志消息打印两次 - Python logging messages get printed twice when importing package that use logging themselves 为什么在 python 中导入模块时出现“ModuleNotFoundError: No module named 'YEETER'”? - Why do I get “ModuleNotFoundError: No module named 'YEETER'” when importing a module in python? 为什么在 Python 中导入自动生成的 gRPC 代码时出现错误? - Why do I get an error when importing my auto-generated gRPC code in Python? 为什么打印的字符在此python代码中消失了? - Why do printed characters disappear in this python code? Python:为什么要打印回溯? - Python: Why do is the traceback getting printed? 为什么 python 字典的元素没有按顺序打印 - why elements of python dictionary not get printed in sequence 当我在Python中插入2行时,为什么会得到无限循环? - Why do I get endless loop when I insert 2 lines in Python? 如何使用 python 将控制台中的打印行附加到 QPlainTextEdit 中? - How do I append printed lines from console into a QPlainTextEdit with python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM