我想将python脚本导入到另一个脚本中.
$ cat py1.py
test=("hi", "hello")
print test[0]
$ cat py2.py
from py1 import test
print test
Run Code Online (Sandbox Code Playgroud)
如果我执行py2.py:
$ python py2.py
hi
('hi', 'hello')
Run Code Online (Sandbox Code Playgroud)
无论如何我可以静音第一个print来自的from py1 import test?
我不能评论的print的py1,因为它正在使用其他地方.
py1.py使用 if __name__=="__main__":
就像你的py1.py看起来像:
def main():
test=("hi", "hello")
print test[0]
if __name__=="__main__":
main()
Run Code Online (Sandbox Code Playgroud)
这将允许您仍然正常使用py1.py,但是当您导入它时,main()除非您调用它,否则它将不会运行该函数.
| 归档时间: |
|
| 查看次数: |
563 次 |
| 最近记录: |