Home > other >  Python exec and subprocess execution script problem.
Python exec and subprocess execution script problem.

Time:11-02

I want to use a script. The main py calls another script run. Py
 # main. Py 
The import subprocess
The import sys

Def fn () :
Print (' I am fn... ')

Fpath='/Users/runtest/run. The p y'

Def run_with_exec () :
"' in the exec run" '
With the open (fpath, 'r', encoding="utf8") as f:
Code=f.r ead ()
The exec (the compile (code. Encode (" utf-8 "), fpath, 'the exec), globals ())

Def run_with_subprocess () :
"' to run subprocess way" '
Subprocess. The run ([sys. The executable, fpath]) # Just run the program

If __name__=="__main__" :
Run_with_exec ()
Run_with_subprocess ()

Use run_with_exec () run globals can be handed in, () is called the script run. Py can use
In the main. Py fn function
But with run_with_subprocess () operation, how to make the called scripts run. Py can use main. Py of fn function?
Have a great god help to look at, thank you very much!
  • Related