【问题标题】:Why can't function defined in vscode be called?为什么不能调用vscode中定义的函数?
【发布时间】:2020-10-28 16:35:49
【问题描述】:

我在 Visual Studio Code 上用 Python 定义了一个函数,但是当我调用它时,会出现以下错误消息:

trin : The term «trin» is not recognized  applet of command name,
       function, script file or program executable. Check name's 
       spelling, or if an access path exists, check that the access 
       path is correct and try again.
At character Line:1 : 1
+ trin(1, 1, 1)
+ ~~~
    + CategoryInfo          : ObjectNotFound: (trin:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException 

我的脚本是:

def trin(a1, b1, a2, b2) :
    if a1 == a2 and b1 == b2 :
        print("The straight lines conincide.")
    elif (a1*b2)-(a2*b1)==0 and a1!=a2 or b1!=b2 :
        print("There is not intersections, the straight lines are parallel")
    else :
        x = (b2-b1)/(a2-a1)
        print("The intersection point is (", x, " ; ", a1*x+b1, ")" )

我删除并重新安装了 vscode、Python 和我的其他 IDE/解释器。

【问题讨论】:

  • 您没有在 Python 提示符下键入 trin(1, 1, 1),我认为错误消息来自 PowerShell。

标签: python visual-studio-code


【解决方案1】:

您的帖子建议您尝试直接从终端而不是从 Python 文件调用函数 trin。您的函数不能被您的操作系统直接调用,这就是它给您此错误的原因。

假设你的代码在一个名为script.py的文件中,如果你想运行这个函数,你需要在你的文件中调用它,而不是直接在终端中调用。

所以我建议将您的代码编辑为以下内容:

# script.py

def trin(a1, b1, a2, b2) :
    if a1 == a2 and b1 == b2 :
        print("The straight lines conincide.")
    elif (a1*b2)-(a2*b1)==0 and a1!=a2 or b1!=b2 :
        print("There is not intersections, the straight lines are parallel")
    else :
        x = (b2-b1)/(a2-a1)
        print("The intersection point is (", x, " ; ", a1*x+b1, ")" )

# This will call your function with the given arguments
trin(0, 0, 1, 1)

然后,在您的终端中,您可以使用以下命令运行您的 Python 脚本:

$ python script.py

这将运行您的脚本,该脚本将调用trin 函数。

【讨论】:

    【解决方案2】:

    您将 python 代码作为 powershell 命令执行,根据您获得的输出非常清楚。因此,在 vs code 中,当你启动它时,你需要像下面这样设置 python 环境:

    • 在左上角的 vs 代码菜单中点击 view >> Command Palette >> Python: Select Interpreter 然后点击您将看到的 python 版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-16
      • 2020-05-25
      • 2012-05-23
      • 2013-06-24
      • 1970-01-01
      • 2010-11-06
      • 2017-12-25
      • 1970-01-01
      相关资源
      最近更新 更多