我有一个名为 test1.py
的脚本,它不在模块中。它只有在脚本本身运行时应该执行的代码。没有函数、类、方法等。我有另一个作为服务运行的脚本。我想从作为服务运行的脚本中调用 test1.py
。
例如:
文件 test1.py
:
print "I am a test"
print "see! I do nothing productive."
文件 service.py
:
# Lots of stuff here
test1.py # do whatever is in test1.py
我知道一种打开文件、读取内容并对其进行基本评估的方法。我假设有更好的方法来做到这一点。或者至少我希望如此。
原文由 Josh Smeaton 发布,翻译遵循 CC BY-SA 4.0 许可协议
执行此操作的通常方法如下所示。
测试1.py
服务.py