-1

So I wrote a program that can manipulate excel files. What I like is to run the script in an interpreter and to call the functions from the shell, and that works fine. What I want to do, is doing the same on a computer that doesn't necessarily have pyhthon installed, so I convererted my py to exe using auto-py-to-exe, but in the exe file you can't access shell.

I've made à 30s video about the issue : https://photos.app.goo.gl/XAaJYikmpcLV6HqC7

code would looke like this :

def main():
    #stuff
    return 

while 1 != 0:
    user = input("What do you want to do ? : ")
    subprocess.call(user, shell=True)

If the user enter "main()" I want it to execute the main function as it would in the shell.

I've tried subprocess.call .run .popen none of it work and os.system stuff also.

EDIT: I want to run any function with any arguments, it can be "main()", "main(1,/path)", "getfiles()", ect. So I dont have to manually write "if user == ...: main(...)" each time.

Vectir
  • 1
  • 2
  • `if user == "main()": main()`? You might consider researching python click. – KamilCuk Feb 21 '23 at 11:54
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Feb 21 '23 at 12:31
  • Sorry, I meant to run any function so I dont have to manually write ` if user == "main()": main() ` for each possible function and arguments. Post edited @KamilCuk – Vectir Feb 21 '23 at 14:11
  • Does https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string answer your question? Second answer. However, consider https://click.palletsprojects.com/en/7.x/commands/ – KamilCuk Feb 21 '23 at 14:23
  • @KamilCuk I found the solution thanks to ChatGPT, the solution is obvious in reality. Just use the "eval" function. If a = input("Your command : "), eval(a) will exécute the command – Vectir Feb 21 '23 at 14:52
  • 1
    Eval is one letter away from evil. – KamilCuk Feb 21 '23 at 15:28

1 Answers1

0

Found the answer :

    def main():
    #stuff
    return 

while 1 != 0:
    user = input("What do you want to do ? : ")
    eval(input)

But WARNING this allow the user to run any command so big security issue

the "exec" command can also help you

Vectir
  • 1
  • 2