0

Im trying to run another file from my tkinter login system but i cant find the way to do it

Ive tried import file

if (p == '123') and (u == 'test'):
    import correct

Im trying to run the file when those are true

albertjjj
  • 37
  • 3
  • 4
    Don't "run the file". Import a function from it at at the beginning of your code, and call that function inside that `if` statement (though what you wrote should technically work if that file contains global code, so I guess you've got some other problem in your code). – goodvibration May 28 '19 at 06:58
  • 4
    Possible duplicate of [What is the best way to call a script from another script?](https://stackoverflow.com/questions/1186789/what-is-the-best-way-to-call-a-script-from-another-script) – Jeppe May 28 '19 at 07:00
  • I agree with the first answer, if your point is to use functions / classes / variables... from another file. But still if you REALLY want to run your module you can with `os.system('py your_module.py')` – Nqsir May 28 '19 at 07:07

3 Answers3

3

One hacky way to do it, if you really have to is this:

with open("correct.py", "r") as f:
    exec(f.read())

But you should avoid doing this if possible. Normally it is better import the file and run a function from it.

LeopardShark
  • 3,820
  • 2
  • 19
  • 33
2

If you want to run a file separately without importing it do this.

from os import system
#system calls a command from terminal
system("python <path to python file>")
PrinceOfCreation
  • 389
  • 1
  • 12
Dadu Khan
  • 369
  • 2
  • 16
0

you can use execfile(filepath) to get run

mino
  • 36
  • 5