首页 > 解决方案 > 交互式python解释器的欢迎信息来自哪里?

问题描述

python在 Linux shell 上输入时,会打印欢迎消息:

[root@localhost ~]# python
Python 2.7.5 (default, Nov 20 2015, 02:00:19)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

这些线是从哪里来的?它们是在编译或安装期间确定的吗?

我的系统上有另一个版本的python可执行文件和一组库,但是当我输入它时python,它也会显示与上面相同的欢迎消息。

谢谢,

更新:

我使用绝对路径来启动另一个版本的 python。刚刚发现欢迎信息与sys.version和sys.platform内容相同。但是如果我将其他版本的 python 复制到不同的 Linux 机器 B,并且仍然使用绝对路径来运行它。我明白了

Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.

此欢迎信息与机器 B 的 python 相同。

标签: python

解决方案


编辑:C版源码类似: https ://github.com/python/cpython/blob/7e4db2f253c555568d56177c2fd083bcf8f88d34/Modules/main.c#L705

if (!Py_QuietFlag && (Py_VerboseFlag ||
                    (command == NULL && filename == NULL &&
                     module == NULL && stdin_is_interactive))) {
    fprintf(stderr, "Python %s on %s\n",
        Py_GetVersion(), Py_GetPlatform());
    if (!Py_NoSiteFlag)
        fprintf(stderr, "%s\n", COPYRIGHT);
}

Py_GetVersion()返回基于 MACRO 的版本

https://github.com/python/cpython/blob/7e4db2f253c555568d56177c2fd083bcf8f88d34/Include/patchlevel.h#L26

/* Version as a string */
#define PY_VERSION          "3.7.0a0"

所以它是编译时间确定的,你可能有一个搞砸的PATH?


旧答案,实际上只是一个 python 模块

https://github.com/python/cpython/blob/7e4db2f253c555568d56177c2fd083bcf8f88d34/Lib/code.py#L214

    if banner is None:
        self.write("Python %s on %s\n%s\n(%s)\n" %
                   (sys.version, sys.platform, cprt,
                    self.__class__.__name__))
    elif banner:
        self.write("%s\n" % str(banner))

不确定这是否回答了您的问题,但知道仍然很有趣。


推荐阅读