Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python how to programmatically print the first two lines the REPL prints everytime is started

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading