one python file run another python file

one python file run another python file

亲测可用

https://stackoverflow.com/questions/7974849/how-can-i-make-one-python-file-run-another

There are more than a few ways. I’ll list them in order of inverted preference (i.e., best first, worst last):

  1. Treat it like a module: import file. This is good because it’s secure, fast, and maintainable. Code gets reused as it’s supposed to be done. Most Python libraries run using multiple methods stretched over lots of files. Highly recommended. Note that if your file is called file.py, your import should not include the .py extension at the end.
  2. The infamous (and unsafe) exec command: execfile(‘file.py’). Insecure, hacky, usually the wrong answer. Avoid where possible.
  3. Spawn a shell process: os.system(‘python file.py’). Use when desperate.

发表评论

电子邮件地址不会被公开。 必填项已用*标注