如何在PyCharm中运行包含同一项目中另一个脚本的脚本? (两个脚本都在PyCharm中)

时间:2019-08-08 16:32:23

标签: python pycharm

我在PyCharm中有一个可执行脚本,该脚本提供了一些定义某些变量的输入。我在同一项目中还有另一个脚本,第二个脚本使用先前定义的变量执行任务并返回信息。

PyCharm是否可以轻松实现此目的,或者它涉及使用第一个脚本顶部的import语句来正式导入文件?如果涉及导入,那么我该如何在第一个脚本中调用第二个脚本?

我的目标是在第一个脚本中运行第二个脚本。理想情况下,以比将整个第二个脚本粘贴到第一个脚本更优雅的方式。

1 个答案:

答案 0 :(得分:0)

我不认为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.pysecond_script.py应该在同一目录中。然后,您必须配置PyCharm,以使其执行first_script.py