How to run Python commands inside cmd using a script

I want to create an executable that opens a cmd and after that insert a clear() function automatically so that I can clear the screen whenever necessary just by typing clear(). Fiz:

import os

os.system('python')

So it opens python in cmd, but after that I can't run any more lines running this script.

How can I write the clear () function inside this python terminal that I just opened using that same script, so that when I run the script, the open terminal configured with clear function and ready for quick use of python?

def clear():
 os.system('cls')
Author: Yone, 2020-06-20

1 answers

What if you do the opposite?

You don't need to start the Python interpreter from a program just to insert a function into it in marra and then take control.

It is easier to start the interpreter normally, using the command python and let it turn around to find and compile its function (or functions).

This is similar to a Linux shell boot file, such as .bash_profile. Whatever is in the file will be run whenever you open the shell.

How to do this in Python?

Setting the environment variable PYTHONSTARTUP to point to the file with Extension .py where its function is.

For example, you can create the file .pythonstartup.py in your user folder with the following content:

import os

def clear():
    os.system('cls')

Then just configure the environment variable in the system properties or directly on the command line:

set PYTHONSTARTUP=%userprofile%/.pythonstartup.py 

So every time you start the Python interpreter, the function clear() will be available. You can also add other useful functions in this file.

 0
Author: Guilherme Brügger, 2020-06-25 01:16:00