Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I find the first text that loads in the Python shell and change it?

Tags:

python

This text loads when I open IDLE or load Python in cmd:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information.

Where can I find the file and change the text or make a script load instead?

like image 846
Coco Avatar asked Apr 29 '15 20:04

Coco


People also ask

What command is used to output text from both the Python shell?

print() is the command you are looking for. The print() function, formally print statement in Python 2.0, can be used to output text from both the python shell and within a python module.

What is the Python shell console?

The Python interactive console (also called the Python interpreter or Python shell) provides programmers with a quick way to execute commands and try out or test code without creating a file.

How do you access the console in Python?

The console appears as a tool window every time you choose the corresponding command on the Tools menu. You can assign a shortcut to open Python console: press Ctrl+Alt+S , navigate to Keymap, specify a shortcut for Main menu | Tools | Python or Debug Console.


Video Answer


2 Answers

I don't know of any way to change the default text without modifying/recompiling the python binary, but it seems you can use the environment variable PYTHONSTARTUP in order to add additional text via a python file with print commands. You can also change the prompt strings in this file. For example:

in my .bashrc:

export PYTHONSTARTUP=/home/jake/.mypythonstartup

/home/jake/.mypythonstartup:

import sys
print("Welcome, master!")
sys.ps1 = "How may I serve you? "
sys.ps2 = "                 ... "

Result:

$ python
Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Welcome, master!
How may I serve you? def test():
                 ...     print("test")
                 ...
How may I serve you? test()
test
How may I serve you? 

Documentation on PYTHONSTARTUP can be found here: https://docs.python.org/3/tutorial/appendix.html#the-interactive-startup-file

like image 129
Jake Griffin Avatar answered Oct 13 '22 20:10

Jake Griffin


Based on a quick snoop around the idlelib source code, you could do something like:

from code import interact
interact("Welcome master.")

In use:

$ python idle2.py
Welcome master.
>>> print 'foo'
foo

You could also use the command line flags to run a command then enter interactive mode:

$ python -ic "print 'Welcome master.'"
Welcome master.
>>> 
like image 33
jonrsharpe Avatar answered Oct 13 '22 19:10

jonrsharpe