在python中运行python代码,就像在编译时从bash运行

时间:2018-04-07 08:01:00

标签: python pyinstaller

我有下一个代码:

main1.py:

from local_module import func

if __name__ == '__main__':
    func()
    # change dir to path_to_main2
    # dynamically import main2.py
    # here I want to run main2.main() in the new environment
    # e.g.: call subprocess.call(['python' 'main2'])
    # but the problem is described in the question!

main2.py(它位于另一个目录中):

# this is not that local_module above, but it has the same name
from local_module import func

def main():
    func()

if __name__ == '__main__':
    main()

所以问题是下一个:

我想编译(使用PyInstaller)这个没有安装python的Windows代码。因此,据我所知,subprocess.call(['python' 'main2'])在编译后无法工作。但是如果我在main2.main()中运行main1.py,我将会导入local_modulefunc()以及另一个func()(本地一个,但不会来自{{} 1}})将被执行。我该如何处理?

P.S。我无论如何都无法修改/编辑main2以避免名称交叉,我无法预测是否会有名称交叉点

0 个答案:

没有答案