0

I have to call a long run process from a bottle script (web framework like Django).

So in a bottle script i have

os.system("python /home/tom/Documents/proc_test/main.py")

Anyway the main.py script is not executed. How can i call a python script from another running python program?

P.S: in python shell interpreter i have write os.system("python /home/tom/Documents/proc_test/main.py") and the file is called

Tom
  • 4,007
  • 24
  • 69
  • 105
  • 6
    It's python. Import it and call the function. – dirkgroten Feb 17 '20 at 13:14
  • Did you try to `exec` it ? `exec(open("/home/tom/Documents/proc_test/main.py").read())` It will work only if the file is executable though. Not related, but I prefer to use the `subprocess` module to execute binaries. – Frodon Feb 17 '20 at 13:18
  • 2
    @Frodon could you avoid suggesting the worse possible solution ? – bruno desthuilliers Feb 17 '20 at 13:24
  • "the main.py script is not executed" => then you may want to find out why. "How can i call a python script from another running python program?" => just like for any other executable - using `os.system` or `subprocess`. But unless you have a compelling reason to run it as a distinct process, you should probably just import the script as a module and call it's entry point (assuming it's correctly written so it can be used that way). – bruno desthuilliers Feb 17 '20 at 13:28
  • Does this answer your question? [Call a python script from a python script within the same context](https://stackoverflow.com/questions/59355847/call-a-python-script-from-a-python-script-within-the-same-context) – guidot Feb 17 '20 at 13:34

2 Answers2

0

Like Dirk suggested, import the main.py from python then call the function.

Or if you need to actually run main.py as a separate process then use subprocess module.

import subprocess
subprocess.call(["python /home/tom/Documents/proc_test/main.py"])

Hope this helps

Tarique
  • 1,273
  • 9
  • 15
  • How is this different from `os.system` ? – Maurice Meyer Feb 17 '20 at 13:22
  • os.system() executes the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations. Changes to sys.stdin, etc. are not reflected in the environment of the executed command. If command generates any output, it will be sent to the interpreter standard output stream. On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent. – Tarique Feb 17 '20 at 13:31
  • The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using os.system() – Tarique Feb 17 '20 at 13:32
  • I meant: Why should `python /home/tom/Documents/proc_test/main.py`run via subprocess if it does not via os.system() ? – Maurice Meyer Feb 17 '20 at 13:35
0

You can import the python script directly via full path using importlib.
Lets assume this example main.py:

def func():
    print("func called")

In your main script, you import as follows:

import importlib as imp
spec = imp.util.spec_from_file_location('d', '/home/tom/Documents/proc_test/main.py')
foo = imp.util.module_from_spec(spec)
spec.loader.exec_module(foo)

foo.func()

Output:

func called
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47