在Vscode终端运行编码器时,它说我的变量不确定

发布于 2025-02-10 16:03:55 字数 505 浏览 1 评论 0原文

我在vscode中写了一个简单的代码,但是当我在终端中运行它时,错误会弹出错误,说该变量未定义。但是,当我手动按“运行代码”按钮时,代码无任何错误运行。

代码:

name = input("what is your name? ")
print("hello,", name)

错误消息:

$ python hello.py
what is your name? ted
Traceback (most recent call last):
  File "hello.py", line 1, in <module>
    name = input("what is your name? ")
  File "<string>", line 1, in <module>
NameError: name 'ted' is not defined

I wrote a simple piece of code in VScode but when I run it in the terminal an error pops up saying the variable is not defined. however when I manually press the run code button the code runs without any errors.

The code:

name = input("what is your name? ")
print("hello,", name)

the error message:

$ python hello.py
what is your name? ted
Traceback (most recent call last):
  File "hello.py", line 1, in <module>
    name = input("what is your name? ")
  File "<string>", line 1, in <module>
NameError: name 'ted' is not defined

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时浪漫 2025-02-17 16:03:55

看来您在计算机上安装了多个Python版本,并且计算机的默认Python版本应为Python2(也许与系统环境变量有关)。您可以使用py -v检查版本。

建议您使用虚拟环境,并且在每个虚拟环境中仅保留一个Python版本。当您选择解释器时( ctrl + shift + p - &gt; python:选择解释器),新的终端( ctrl + shift + `)将自动激活虚拟环境。

虚拟环境对您使用解释器和特定软件包的特定版本非常有用。

为了防止这种混乱,开发人员经常为项目创建虚拟环境。虚拟环境是包含特定解释器的副本(或符号链接)的文件夹。当您安装到虚拟环境中时,您安装的任何软件包仅在该子文件夹中安装。然后,当您在该环境中运行Python程序时,您就会知道它仅针对那些特定的软件包。

如何创建虚拟环境:

  1. 创建虚拟环境

      python -m venv&lt; name_of_envirnment&gt;
     
  2. 激活虚拟环境

     &lt; name_of_envirnment&gt; \ scripts \ activate
     

官方文档:创建一个虚拟环境

It looks like you have multiple python versions installed on your machine, and your computer's default python version should be python2 (perhaps related to system environment variables). You can check the version with py -V.

It is recommended that you use a virtual environment and keep only one python version in each virtual environment. When you select an interpreter ( Ctrl + Shift + P --> Python: Select Interpreter), the new terminal ( Ctrl + Shift + ` ) will automatically activate the virtual environment.

Virtual environments are very useful for you to use a specific version of the interpreter and a specific package.

To prevent such clutter, developers often create a virtual environment for a project. A virtual environment is a folder that contains a copy (or symlink) of a specific interpreter. When you install into a virtual environment, any packages you install are installed only in that subfolder. When you then run a Python program within that environment, you know that it's running against only those specific packages.

How to create a virtual environment:

  1. Create Virtual Environment

    python -m venv <name_of_envirnment>
    
  2. Activate Virtual Environment

    <name_of_envirnment>\scripts\activate
    

official documentation: Create a virtual environment

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文