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

5
  • 6
    It's python. Import it and call the function. Commented Feb 17, 2020 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. Commented Feb 17, 2020 at 13:18
  • 2
    @Frodon could you avoid suggesting the worse possible solution ? Commented Feb 17, 2020 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). Commented Feb 17, 2020 at 13:28
  • Does this answer your question? Call a python script from a python script within the same context Commented Feb 17, 2020 at 13:34

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

How is this different from os.system ?
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.
The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using os.system()
I meant: Why should python /home/tom/Documents/proc_test/main.pyrun via subprocess if it does not via os.system() ?
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.