简体   繁体   English

交互式python解释器的欢迎消息来自何处?

[英]where does the welcome message of interactive python interpreter come from?

When entering python on Linux shell, the welcome message is printed: 在Linux Shell上输入python时,将显示欢迎消息:

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

where do those lines come from? 这些线从哪里来? Are they determined during compilation or installation? 它们是在编译或安装过程中确定的吗?

I have another version of python executable and a set of libs on my system, but when I enter that python , it also shows the same welcome message as above. 我的系统上有另一个版本的python可执行文件和一组库,但是当我输入python ,它也显示与上述相同的欢迎消息。

Thanks, 谢谢,

UPDATE: 更新:

I use absolute path to start another version of python. 我使用绝对路径启动另一个版本的python。 And just found the welcome message has the same content as sys.version and sys.platform. 刚刚发现欢迎消息的内容与sys.version和sys.platform相同。 But if I copy the other version of python to a different Linux machine B, and still use absolute path to run it. 但是,如果我将其他版本的python复制到另一台Linux计算机B,并且仍然使用绝对路径来运行它。 I get 我懂了

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.

This welcome message is the same as machine B's python. 此欢迎消息与机器B的python相同。

Edited: The C version source code is similar: https://github.com/python/cpython/blob/7e4db2f253c555568d56177c2fd083bcf8f88d34/Modules/main.c#L705 编辑: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);
}

which Py_GetVersion() returns version base on a MACRO 哪个Py_GetVersion()返回基于MACRO的版本

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

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

so it is compile time determined, you probably have a messed up PATH? 所以它是由编译时间决定的,您可能有一个混乱的PATH吗?


Old answer, which is actually just a python module 旧答案,实际上只是一个python模块

https://github.com/python/cpython/blob/7e4db2f253c555568d56177c2fd083bcf8f88d34/Lib/code.py#L214 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))

Not sure if this answers your question, but still fun to know. 不知道这是否能回答您的问题,但仍然很高兴知道。

I've finally found the reason. 我终于找到了原因。 The second python binary loads .so files in startup, and it loads libpython as follows: 第二个python二进制文件在启动时加载.so文件,并按如下方式加载libpython:

libpython2.7.so.1.0 => /lib64/libpython2.7.so.1.0 (0x00007f087cf58000)

This is the same as my system python . 这与我的系统python相同。 After setting LD_LIBRARY_PATH to the lib directory of the second python , I can see the correct welcome message. LD_LIBRARY_PATH设置为第二个python的lib目录之后,我可以看到正确的欢迎消息。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM