简体   繁体   English

从另一个 python 脚本运行 python 脚本

[英]run a python script from another python script

i have a test script test.py inside which i am trying to run one more python script bexec.app (this is a python script).我有一个测试脚本 test.py 在里面我试图再运行一个 python 脚本bexec.app (这是一个 python 脚本)。 Actually test.py is just for my testing.实际上test.py只是为了我的测试。 The original script is a lengthy one and bexec.app is getting called inside in a command line.原始脚本很长,并且bexec.app在命令行内部被调用。

I tried this我试过这个

#!/usr/bin/python
import os, sys
os.path.insert(0, 'current bexec script path')
import bexec.app

But this does not work -但这不起作用 -

Traceback (most recent call last):
  File "test", line 5, in <module>
    import bexec.pphshaper
ImportError: No module named bexec.app

I donot want to change the bexec.app script to bexec_app.py because this follows some naming convention我不想将 bexec.app 脚本更改为 bexec_app.py 因为这遵循一些命名约定

bexec.app also has #!/usr/bin/python inside it. bexec.app 里面也有#!/usr/bin/python how can i import my bexec script inside test.py我如何在test.py中导入我的bexec脚本

You seem to want to run your bexec, not import it.您似乎想运行您的 bexec,而不是导入它。 And if so, regular如果是这样,定期

os.system('current bexec script path')

should be sufficient应该足够了

Dot means subpackage structure to python, but it's still possible with imp .点表示 python 的子包结构,但imp仍然可以。

below is bexec.app sample file:下面是bexec.app示例文件:

#!/usr/bin/python

a = 2

and this is how you can import it in your test.py file, look below for the code:这就是你如何在你的test.py文件中导入它,在下面查看代码:

#!/usr/bin/python
import os, sys, imp

my_module = imp.load_source('my_module', 'bexec.app')

print (my_module.a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM