Play with Python's interactive (command line) mode

I like to use Python's interactive interface (command line mode) to run and debug Python code. Why not use PyCharm, VSCode? Because of preconceived ideas and my DOS command line complex, the first time I installed and used Python was to use this black and white interface. I usually write code using EmEditor editor, save it and run and debug it in Python. This black-and-white interface is fast in both the speed of text output and the efficiency of code operation! Very cool! It's been cool to write code all the time! This article is my summary of the experience of playing with the Python interactive interface.

1. Run the .py code directly

exec(open(r'd:\py\test.py',encoding='utf-8').read())

It is inconvenient to type such a long sentence every time. In fact, just type ex and press the up key to quickly call this line of command.

Is it possible to make Python remember the .py file I ran last time every time I open Python? Pay attention to the condition I said: every time you open Python.

Can. After this running .py finishes, press Ctrl + D (or type exit() ) to exit Python. Then open Python next time, type ex and press the up key, the file name in open() will be the .py path opened last time.

 

2. Clear the screen display (equivalent to the CLS command of DOS)

按Ctrl + L

For more shortcut key operations, please refer to this article: https://jakevdp.github.io/PythonDataScienceHandbook/01.02-shell-keyboard-shortcuts.html

 

3. Quickly query the executed statements

        (1) Direct query for recently used statements

按键盘的上、下方向键,逐条查询。

        (2) Fuzzy query by keyword

按 Ctrl + R
然后输入某个关键词即可找到最近一次使用该关键词的语句。

 

4. Change the interface background color

The method is the same as modifying the color of the Windows command prompt (CMD): click the Python icon in the upper left corner of the window --> Properties --> Color. I like to change the background color to dark blue and the color values ​​are: R 0, G 15, B 50.

 

 

5. Change to a better-looking font

The default Song typeface looks rough and shabby. I recommend installing Sarasa Fixed SC from the Gengsha font library. The "1", uppercase letter "I" and lowercase letter "l" of this monospaced font can be clearly distinguished. The word 0 has a slash, which is full of nostalgic feeling of the DOS era.

 

Sarasa-Gothic font is an open source font library, which can be downloaded from here: Releases be5invis/Sarasa-Gothic GitHub

On the download page, I personally recommend downloading the ttc version with unhinted. Gengsha font has a black font Chinese font library, and the font has no hinting information, and the display is smoother in systems above Win10, unlike Microsoft Yahei, which has hinting information that causes small fonts to feel uneven and awkward.

 

6. The prompt is changed to a more eye-catching color

Reference source:

(1)https://www.codementor.io/@arpitbhayani/personalize-your-python-prompt-13ts4kw6za

(2)https://github.com/arpitbbhayani/py-prompts

The default prompt ">>>" is white, and when the screen displays complicated information, it is often impossible to find the place where the statement was executed last time. In fact, the prompt of Python's interactive interface can modify the color and even the style.

import sys
sys.ps1 = "\033[1;32m>>>\033[0m "
sys.ps2 = "\033[1;31m...\033[0m "

Like this, sys.ps1 modifies ">>>" to be green, and sys.ps2 modifies "..." to be red.

Running the above modified color code in the interactive interface will only be effective for the current time. Next time you open the Python interactive interface, the color will return to the default white. In order to make it automatically run and modify the color code every time, you need to do this:

(1) Save the three lines of code above import sys, sys.ps1..., sys.ps2... as a .py file named run_me_first.py.

(2) Put the run_me_first.py file in the main directory of Python (eg: C:\Python310)

(3) Press Win+Pause (or enter "System" in the control panel), then click "Advanced System Settings" on the left, and click "Environment Variables".

 

(4) Click "New" under "System Variables" to create a variable name: PYTHONSTARTUP, the variable value is the detailed path and file name of the .py file just saved, and click OK. The next time you run Python, the color of the prompt will remain in effect.

 

Guess you like

Origin blog.csdn.net/Scott0902/article/details/129402979