Skip to content Skip to sidebar Skip to footer

How To Run A Python Script From Another Python Script In The Cross-platform Way?

Here is the problem... I'm writing very small plugin for Blender, I have 10 python scripts, they parsing different file formats by using command-line, and I have a Main Python scri

Solution 1:

If you really need to execute a python script in a new process and you don't know where the interpreter you want is located then use the sys module to help out.

import sys
import subprocess

subprocess.Popen((sys.executable, "script.py"))

Though importing the module (dynamically if need be) and then running its main method in another script is probably a better idea.

Solution 2:

for example, "Main.py" include:

txt2cfg.py -inFile -outFile... ma2lxo.py -inFile -outFile...

Two things.

  1. Each other script needs a main() function and a "main-import switch". See http://docs.python.org/tutorial/modules.html#executing-modules-as-scripts for hints on how this must look.

  2. Import and execute the other scripts.

    import txt2cfg
    import ma2lxo
    txt2cfg.main( inFile, outFile )
    ma2lxo.main( inFile, outFile )
    

This is the simplest way to do things.

Solution 3:

Two options:

  1. Use py2exe to bundle the interpreter with the scripts.
  2. Import the modules and call the functions automatically.

Post a Comment for "How To Run A Python Script From Another Python Script In The Cross-platform Way?"