python3 --version 显示“NameError: name 'python3' is not defined"

2022-01-23 00:00:00 python command

问题描述

当我们输入时

python3 --version (or --V)

它应该向我们展示python的版本对吗?

it is supposed to show us the version of the python right?

但是,当我这样做时,我收到以下错误:

However, when I do this I get the following error:

NameError: name 'python3' is not defined

NameError: name 'python3' is not defined

我尝试使用安装pip时也是这种情况

This is also the case when I tried to install the pip by using

>>> python3 get-pip.py
  File "<stdin>", line 1
    python3 get-pip.py
              ^
SyntaxError: invalid syntax


解决方案

python3 不是 Python 语法,它是 Python 二进制文件本身,你运行到交互式解释器的东西.

python3 is not Python syntax, it is the Python binary itself, the thing you run to get to the interactive interpreter.

您将 命令行 与 Python 提示符混淆了.打开控制台 (Windows) 或终端 (Linux、Mac),您可以使用 dirls 从命令行浏览文件系统.

You are confusing the command line with the Python prompt. Open a console (Windows) or terminal (Linux, Mac), the same place you'd use dir or ls to explore your filesystem from the command line.

如果您在 >>>In [number]: 提示符处键入,则说明您来错地方了,那就是 Python 解释器本身和它只需要 Python 语法.如果您从命令行启动 Python 提示符,则退出此时并返回命令行.如果您从 IDLE 或在 IDE 中启动解释器,则需要将终端或控制台作为单独的程序打开.

If you are typing at a >>> or In [number]: prompt you are in the wrong place, that's the Python interpreter itself and it only takes Python syntax. If you started the Python prompt from a command line, exit at this point and go back to the command line. If you started the interpreter from IDLE or in an IDE, then you need to open a terminal or console as a separate program.

人们经常混淆 Python 语法的其他程序;其中每一个实际上都是在命令提示符下运行的程序:

Other programs that people often confuse for Python syntax; each of these is actually a program to run in your command prompt:

  • pythonpython2.7python3.5
  • pippip3
  • 虚拟环境
  • ipython
  • easy_install
  • django-admin
  • conda
  • 烧瓶
  • scrapy
  • setup.py -- 这是您需要使用 python setup.py [...] 运行的脚本.
  • 以上任何一项连同 sudo.
  • python, python2.7, python3.5, etc.
  • pip or pip3
  • virtualenv
  • ipython
  • easy_install
  • django-admin
  • conda
  • flask
  • scrapy
  • setup.py -- this is a script you need to run with python setup.py [...].
  • Any of the above together with sudo.

根据您安装的工具和库以及您要执行的操作,可能会有更多变化.

with many more variations possible depending on what tools and libraries you have installed and what you are trying to do.

如果给定参数,您将得到 SyntaxError 异常,但根本原因是相同的:

If given arguments, you'll get a SyntaxError exception instead, but the underlying cause is the same:

>>> pip install foobar
  File "<stdin>", line 1
    pip install foobar
              ^
SyntaxError: invalid syntax

相关文章