How to execute Python commands inside cmd using a script

Asked

Viewed 1,350 times

0

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

import os

os.system('python')

So it opens python in cmd, but after that I can no longer run any lines running this script.

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

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

1 answer

0


And if you do otherwise?

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

It is easier to start the interpreter normally using the command python and let him turn to find and compile his function (or functions).

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

How to do this in Python?

Configuring the environment variable PYTHONSTARTUP to point to the file with extension .py where is its function.

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 direct 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 that file.

  • It was perfect! Thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.