简体   繁体   中英

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

When entering python on Linux shell, the welcome message is printed:

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

Thanks,

UPDATE:

I use absolute path to start another version of python. And just found the welcome message has the same content as sys.version and 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. 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.

Edited: The C version source code is similar: 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

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?


Old answer, which is actually just a python module

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:

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

This is the same as my system python . After setting LD_LIBRARY_PATH to the lib directory of the second python , I can see the correct welcome message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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