如何以跨平台方式从另一个Python脚本运行一个Python脚本? [英] How to run a Python script from another Python script in the cross-platform way?

查看:111
本文介绍了如何以跨平台方式从另一个Python脚本运行一个Python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是问题所在...

我正在为Blender编写非常小的插件, 我有10个python脚本,它们通过使用命令行来解析不同的文件格式,并且我有一个Main Python脚本,可以使用适当的命令来运行所有其他脚本...

例如,"Main.py"包括:

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

Blender已经包含Python,因此我可以从Blender中运行"Main.py",但是我需要它可以与PC和MAC一起使用,并且不需要Python安装,因此我不能使用:

  • execfile('txt2cfg.py -inFile -outFile')
  • os.system('ma2lxo.py -inFile -outFile')
  • 甚至导入子流程

因为他们需要安装Python才能运行* .py文件.

对不起,语言

谢谢

解决方案

例如,"Main.py"包括:

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

两件事.

  1. 每个其他脚本都需要main()函数和"main-import开关".请参见 http://docs.python.org/tutorial/modules. html#executing-modules-as-scripts 以获得有关其外观的提示.

  2. 导入并执行其他脚本.

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

这是做事的最简单方法.

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 script to run all other scripts with proper commands...

for example, "Main.py" include:

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

Blender already include Python, so I can run "Main.py" from Blender, But I need it to work with both PC and MAC, and also doesn't require Python installation, so I can't use:

  • execfile(' txt2cfg.py -inFile -outFile ')
  • os.system(' ma2lxo.py -inFile -outFile ')
  • or even import subprocess

because they required Python installation in order to run *.py files.

Sorry for language

Thanks

解决方案

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.

这篇关于如何以跨平台方式从另一个Python脚本运行一个Python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆