【问题标题】:Python doesn't run properly on Visual Studio CodePython 在 Visual Studio Code 上无法正常运行
【发布时间】:2020-09-17 20:50:10
【问题描述】:

我按照https://code.visualstudio.com/docs/python/python-tutorial 的说明下载了 Visual Studio Code 并安装了 Python。

当我在 Visual Studio Code 中选择 Python 扩展时,开始安装,但之后它告诉我不支持系统安装。因此我使用Homebrew 安装它。

首先,我似乎没有上面安装教程中显示的绿色“运行”按钮。在那里我只能通过右键单击和“在 Python 终端中运行选择/行”来运行代码。

一个简单的print('Hello, World!') 似乎可以正常运行,但是当我尝试运行一个包含多行(即多条指令)的程序时,我收到了多个错误消息,这些错误消息是我之前使用Anaconda 时没有得到的.

对于上下文:我目前正在学习使用 Python 进行编程,并遵循“使用 Python 自动化无聊的东西”一书。

我尝试运行的代码是:

#! python3
# mclip.py - A multi-clipboard program.

TEXT = {'agree': """Yes, I agree. That sounds fine to me.""",
'busy': """Sorry, can we do this later this week or next week?""", 'upsell': """Would you consider making this a monthly donation?"""}

import sys, pyperclip

if len(sys.argv) < 2:
    print('Usage: py mclip.py [keyphrase] - copy phrase text')
    sys.exit()

keyphrase = sys.argv[1] # first command line arg is the keyphrase

if keyphrase in TEXT:
    pyperclip.copy(TEXT[keyphrase])
    print('Text for ' + keyphrase + ' copied to clipboard.')
else:
    print('There is no text for ' + keyphrase)

为了澄清: Traceback (most recent call last): File "/Users/XXX/Desktop/Python Projects/Ex.py", line 7, in &lt;module&gt; import sys, pyperclip ModuleNotFoundError: No module named 'pyperclip' 是我得到的错误。

我只是想正确使用 Visual Studio Code,但我真的不知道为什么它不工作或者我在安装它时做错了什么。 (例如,为什么我错过了绿色的“运行”按钮?!)

【问题讨论】:

标签: python visual-studio-code


【解决方案1】:

第 10 行的代码中有一个语法错误。sys.exit() 应该在下一行。修复后,它在我的 Visual Studio Code 中运行良好。

【讨论】:

  • 这里好像是Path有问题。见WARNING: The script isort is installed in '/Users/max/Library/Python/3.7/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. WARNING: The scripts epylint, pylint, pyreverse and symilar are installed in '/Users/max/Library/Python/3.7/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
【解决方案2】:

这是缩进错误:

if len(sys.argv) < 2:
    print('Usage: py mclip.py [keyphrase] - copy phrase text')
    sys.exit()

sys.exit() 应该在下一行。

【讨论】:

【解决方案3】:

要显示绿色播放按钮,您必须在 Visual Studio Code 中安装 Python 扩展。

你犯了一个错误。当我运行它时,我得到了它。您必须在输入后或下一行放置 sys.exit():

sys.exit()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 2022-10-17
    • 1970-01-01
    • 2019-09-11
    • 1970-01-01
    • 2011-07-18
    • 1970-01-01
    相关资源
    最近更新 更多