简体   繁体   English

直接从Spyder中的另一个脚本在Spyder中运行脚本

[英]Run a script in Spyder directly from another script in Spyder

I have a Main script in Spyder that calls several functions contained in 6 different scripts (.py). 我在Spyder中有一个Main脚本,可以调用6个不同脚本(.py)中包含的几个函数。 I had to this way because the scripts are also used in different projects. 我之所以必须这样做,是因为脚本也用在不同的项目中。

Currently, I have to run each script (containing several functions each) separately by hand, which is tiring, by clicking in the "green triangle" before launching the Main Script so that the functions contained in each script are stored in the working environment. 当前,我必须手动运行每个脚本(每个脚本包含多个功能),这很累,通过在启动主脚本之前单击“绿色三角形”,以便将每个脚本中包含的功能存储在工作环境中。

My question is: Would it be possible to automatically run each script directly from the Main Script and not running one after the another by hand? 我的问题是:是否可以直接从主脚本中自动运行每个脚本,而不是手动一个接一个地运行?

When you execute an import statement, the source file being imported is executed. 执行import语句时,将执行要import的源文件。 So, for example, if you have thing.py and you execute import thing , all the code in thing.py will be run. 因此,例如,如果您拥有thing.py并执行import thing ,则将运行thing.py所有代码。

Also, as noted in a comment by Sven Krüger : you can use runpy.run_path , which I think is overall a better solution than my original suggestion. 此外,如由斯文克鲁格评论指出:你可以使用runpy.run_path ,我认为这是整体比我原来的建议,更好的解决方案。

Try 尝试

from filename import *

instead of 代替

import filename

No .py extension in the import. 导入中没有.py扩展名。

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

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