【问题标题】:Python script only working when ran in Visual Studio CodePython 脚本仅在 Visual Studio Code 中运行时才有效
【发布时间】:2021-05-06 20:07:27
【问题描述】:

我的 python 代码只有在放入 Visual Studio Code 时才能运行。代码仍然可以运行,但是当我点击脚本运行它时,它不起作用,当我使用命令运行它时,它也不起作用。

# importing the module
import time
import wikipedia
import io

# looks for a random page and stores the topic in random
random = wikipedia.random(pages=1)

#searches for the topic and stores it in wiki
wiki = wikipedia.summary((random), sentences = 1) 

# print the output of wiki
file = open("test.txt", "w")
file.write(wiki)
file.close

我也有一个 AutoHotkey 脚本来运行它,但这也不起作用。

【问题讨论】:

  • 你能分享它引发的错误吗?
  • 从命令行运行脚本时是否有任何消息?
  • 它不起作用是什么意思?什么都没有,或者你得到一个错误?你的代码看起来不错我猜你的 python 路径错误或类似的东西

标签: python python-3.x visual-studio-code


【解决方案1】:

这可能意味着您没有将 python 添加到路径中。

这就是你如何将 python 添加到路径:

1. Start the Run box and enter sysdm.cpl
2. In the System Properties window go to the Advanced tab and 
   click the Environment Variables button
3. In the System variable window, find the Path variable and 
   click Edit
4. Position your cursor at the end of the Variable value line 
   and add the path to the python.exe file, preceeded with 
   the semicolon character (;)

【讨论】:

  • 谢谢,这已经解决了。我还必须确保 .py 扩展名使用 python 打开,而不是 Visual Code Studio。
  • 这意味着您只能使用一个版本的 Python。
猜你喜欢
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2020-01-02
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 2016-01-01
  • 2013-03-22
相关资源
最近更新 更多