$ python
Python 3.11.1 (main, Jan 4 2023, 23:41:08) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Is there a programmatic way of printing those two lines that get printed when python is started?
Note: Somehow related to Printing Python version in output but not exactly the same question.
CodePudding user response:
You can do this with the following:
import sys
print(f'Python {sys.version} on {sys.platform}')
print('Type "help", "copyright", "credits" or "license" for more information.')
Test on Windows:
>python
Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; print(f'Python {sys.version} on {sys.platform}'); print('Type "help", "copyright", "credits" or "license" for more information.')
Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Test on Linux:
$ python3
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; print(f'Python {sys.version} on {sys.platform}'); print('Type "help", "copyright", "credits" or "license" for more information.')
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
I assume you really want this information so that you can introspect the environment your program is running on (and not just for the sake of printing this information), in which case I suggest you read the documentation for the platform
module.
CodePudding user response:
I just figured sys.version
has most of what goes in the first line, and the second one is really just a literal.
CodePudding user response:
To my knowledge, there is not any specific functionality to print the overall message. The options "help", "copyright", "credits" or "license"
in this message are hard-coded, and not determined by checking through the builtins for those names. The build message, however, does check the version info, and we can emulate that logic using the builtin sys
module:
import sys
def banner():
print(f'Python {sys.version} on {sys.platform}')
print('Type "help", "copyright", "credits" or "license" for more information.')
CodePudding user response:
You can do something similar to this:
import platform
import sys
system = platform.system()
release = platform.release()
version = sys.version.split()[0]
print(f"Python {version} on {system} {release}")
print(sys.copyright)
You can extract all the info you need and thet concat that info in an string