我在 PyCharm 中有一个可执行脚本,给定一些输入定义了某些变量。我在同一个项目中还有另一个脚本,第二个脚本使用先前定义的变量来执行任务并返回信息。PyCharm 是否有办法轻松做到这一点,或者它是否涉及使用第一个脚本顶部的 import 语句正式导入文件?如果涉及导入,那么我将如何在第一个脚本中调用第二个脚本?我的目标是在第一个脚本中运行第二个脚本。理想情况下,以比将整个第二个脚本粘贴到第一个脚本更优雅的方式。
1 回答

沧海一幻觉
TA贡献1824条经验 获得超5个赞
我不认为 PyCharm 有这个功能,因为它不会真的是 Python。您可以执行以下操作:
在first_script.py:
from second_script import my_function
# define certain variables with given some input
x = input()
y = input()
# ...
# call 'my_function', which is defined in 'second_script.py'
result = my_function(x, y)
在文件中second_script.py:
# define a function to process the variables and return something
def my_function(x, y):
# process the variables
z = x + y
# and return the result
return z
这两个文件first_script.py都second_script.py应该在同一个目录中。然后你必须配置 PyCharm 以便它执行first_script.py。
添加回答
举报
0/150
提交
取消