NameError: name ‘python3’ is not defined in Python

NameError: name ‘python3’ is not defined in Python is an error that occurs when developers confuse the command line with the Python prompt. The fix is easy it’s just a matter of syntax.

In the following article I am trying to present to you where the problem is and how you can fix it, I also present to you solution that other developers recommended.

Explaining NameError: name ‘python3’ is not defined in Python

The first time I got the error was when I was starting my Python development journey, after running the code bellow for example in order to check which python version I have installed on my machine

                                                                       #
python3 --v 
                                                                       #

This is the error I get after running the code above

                                                                       #
NameError: name 'python3' is not defined
                                                                       #

This error must be confusing when you are first starting with Python, this article is going to explain the problem and describe a solution, follow the solutions bellow.

Solution 1 : Do not confuse the Python prompt with the command line.

First of all, when you get this error you are 100 percent confusing the Python prompt with the command line.

If you are trying to type commands after >>> or after In 0: you are not where you are supposed to be, you are in the Python interpreter and the interpreter only accepts Python syntax.

Meaning you can not run the code we have seen above there, you can only run that code in the command line in Windows or Linux.

If you started that interpreter from an IDE just open cmd in Parallel in windows for example, if you opened it from the command line just exit and return back to the CMD.

By the way python3 or python2 are not Python syntax, they are the Python binaries, the things you run to get to the interactive interpreter on Python.

You can get the same error by typing not only that but pip, pip3, conda, flask …etc.

The resulting error will always be the same.

I believe the explanation above and the instructions are enough to solve this error as it is not very complicated. If that was not helpful, please check out the other solution bellow, it may help, who knows.

Solution 2 : use import pip

When you have a windows machine you can run this in the Python prompt:

                                                                       #
>>>>import pip
>>>>pip.main(['install', 'foobar'])
                                                                       #

import pip and pip.main() will also work on Linux.

I hope you found this helpful

Summing-up

That is it guys, this is the end of this article aka guide, I hope you found it useful in solving your problem, make sure to support our work on Kofi, you do not have to but hey you can donate to the team.

The error was a matter of syntax, it usually happens when you are just starting but mistakes always happen even to experienced developers, thank you for reading and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/