Syntax Error in Python 3.10 when running on Terminal

Hey guys,
I’m new to Python & I tried to run my 1st Python script (“Hello World”) on the terminal using Python 3.10.
After access to the location of the py file, I tried to run it using:
“Python .py” - but I got the following SyntaxError:
“Leading zeros in decimal integer literals are not permitted; use an 0o for octal integers”.

Thanks

Hello, @PythonGuy you need to input the name of the python file (.py) to terminal.

• Make helloworld.py file with this code below:
print("Hello World")

• Go to the path/location of the file:
cd C:\path\to\file  # Windows
cd ~/path/to/file # Linux

• Execute the file with this command:
python helloworld.py

'Hello World'


https://docs.python.org/3/tutorial/index.html
or beginner free book here:
https://automatetheboringstuff.com/

Thanks but this is what I did exactly and got the syntax error previously mentioned:
C:\Python310\Exfiles>Python helloworld.py

use lowercase python helloworld.py

I did but with the same error

can you show the code of helloworld.py?

and use the <\> toolbar in the editor to highlight your code like this:


type or copy paste your code here


1 Like

I think, just writing helloworld.py will execute the file.(You must be inside the dir -where your file exists- in terminal, as @gunungpw has already mentioned about.)

Hi Gunung, thanks for trying to help, but you have given three answers that don’t have anything to do with the poster’s problem. He is not having trouble getting Python to look at the script, he is getting a SyntaxError, which means that Python has looked at the script and found a syntax error (invalid code) in the file.

PythonGuy, look at the SyntaxError you get:

“Leading zeros in decimal integer literals are not permitted; use an 0o for octal integers”.

The error message will show you the line that is invalid. Somewhere in that line, you have a number written like this:

0123


or something similar, with a leading 0 digit. You cannot use leading zero digits in Python ints. (They are allowed in numbers with a decimal point: 0.123 is allowed.)

To fix the error, remove the leading 0 digit: change 0123 to 123.

The reason for the error is that, in the past, Python copied the C programming language and used numbers with leading zeroes for base-8 “octal” numbers. So in old versions of Python, 0123 would be in octal.

The decimal number 83 is the same as:

• 53 in hexadecimal (base 16), which Python writes as 0x53
• 1010011 in binary (base 2), written as 0b1010011
• 123 in octal (base 8)

In old versions of Python, we used 0123 to mean octal, giving the decimal value 83. But that was confusing: too many people used a leading zero and then got the wrong result.

So Python changed the rules. To write an octal number today, we write it as 0o123 not 0123, and to avoid accidental mistakes the leading zero is prohibited.

1. Read the error, and see which line of code it refers to.

2. Find the number with a leading zero on that line of code.

3. If you intended it to be octal, then change it to use 0o...

4. If the zero was just a placeholder, then just remove it.

@steven.daprano thanks , I just guessed OP problem, and because of that I ask the code of the script. Thank you for actively answered beginner question in forum, i still learning English and programing in general. Hope you have a nice day

Hello, @PythonGuy, and welcome to Python Foundation Discourse!

If you had posted your code here, we could offer more effective help. If you do post code, please make sure it is formatted correctly.

Some examples of types of numbers that can be used in Python programs are integers and real numbers. Examples of integers, correctly expressed for Python, include:

• 42
• 144
• -14

Examples of real numbers include:

• 3.17
• -77.4

The following would not be accepted as integers by Python, because they each have a leading 0:

• 077
• -077

The above would each raise a SyntaxError.

When writing a program, it is important to consider what types of data you are using. A count of some kind of object, such as a number of slices of pizza, would be appropriately expressed as an integer, such as 3. However, if you were to specify the name of something, such as a programming language, you would most likely use a string, of which the following are examples:

• "Python"
• 'Ruby'

Note that the strings are contained in either double or single quotes.

Some entities that are commonly referred to as numbers are not actually numbers. At a bank or on an electronic device, you may be asked for a pin number. Most likely, that pin number should be thought of as a string, rather than as a number, even though it might consist entirely of digits. In many situations, valid pin numbers could begin with a 0. Therefore, if your Python program contains a pin number, you should very likely enclose it in quotes. The following would be valid:

pin_number = "0276"


However, this would raise a SyntaxError:

pin_number = 0276


print (“hello World”)
hello World

Thank you Steven for this elaborated feedback.
My starting script is just 1 line reading:
Print (“Hello World”).

It runs properly on the IDLE where created but with that syntax error on the terminal.

That code would not execute, as is, because print needs to begin with a lowercase p. When posting code, it is necessary to literally copy and paste it, then to format it.

EDIT (March 19, 2022):

It also must be mentioned that the following program would raise a SyntaxError, but not one concerning a leading 0:

Print (“Hello World”)


However, perhaps you had another script named “.py”.

On my MacBook Air, I wrote the following program, and renamed it as above:

# The name is Bond, James Bond.
print(007)


Then, running it from the terminal with the command reported above, part of the result was:

SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers


So, it is possible that you mistakenly activated a script other than the intended one, and that raised the reported error.

Hi Anthony,

There is no possible way to get the SyntaxError you reported from a file with just one line print("Hello World").

Possibly Gunung was correct, and you are running the wrong script.

We cannot help you now unless you copy and paste the full command you give on the terminal and the error you receive.

• What operating system you are using.

• What command you used on the terminal.

• What the result was.

Please don’t summarise it, or re-type from memory. We need to see the exact unedited command and result, otherwise we cannot help you.

Hello Steven;

1. OS: Windows 10 Pro (vers. 21H1)/ 64 bit/ x64-based processor.
2. Python 3.10.
3. Terminal: C:\Python310\Exfiles\python helloworld.py
4. Result:
File “c:\Python310\Exfiles\helloworld.py”, line 1
Python 3.10.3 (tags/v3.10.3:a342a49, Mar 16 2022, 13:07:40) [MSC v.1929 64 bit (AMD64)] on win32
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers.

To note here that the exact same result mentioned above is popping up every time I tried to run each of the 5 one-line scripts I created for testing with 1 single command print(’’).

“c:\Python310\Exfiles>dir” lists for me all the py files created & saved therein and I tried to run them one after the other as described earlier with the exact same result.

The first line of your helloworld.py file says:

Python 3.10.3 (tags/v3.10.3:a342a49, Mar 16 2022, 13:07:40)  [MSC v.1929 64 bit (AMD64)] on win32


Remove it. It is not valid Python code, it is a line of junk that does not belong in a Python script, and the interpreter has got confused by the “07”. By my count, there are at least 15 syntax errors in that junk line, and the interpreter had to pick one to report, it just happened to be the “07” one.

The lessons here:

(1) It helps to look at the full error message Python gives you.

Python will show you the line of the script it thinks is wrong. That helps you to see which line of your script usually contains the error, or at worst, maybe the previous line.

(2) Always double and triple check the file you are running contains what you expect. You told us that the file contained one line, print("Hello World") but that was not correct, there was an extra line of junk at the start of the file.

If you had given us accurate information from the beginning, and shown us the full contents of the file:

Python 3.10.3 (tags/v3.10.3:a342a49, Mar 16 2022, 13:07:40)  [MSC v.1929 64 bit (AMD64)] on win32
print("Hello World")


or the full traceback, we could have answered your question instantly, instead of taking 15 posts and well over a day to solve it.

This is part of learning to be a programmer! We know how to solve these problems because we know how to use the information the interpreter gives us, and because we’ve all made these same errors (or very similar ones) ourselves.

1 Like

Ahaha! And a further mystery is solved.

I am sure that you did not type the line “Python 3.10.3 (tags/v3.10.3:a342a49 blah blah blah…” into your helloworld.py script.

What I believe happened is that you used IDLE. You opened IDLE, and it started by giving you the interpreter window.

The interpreter window starts off by running the interpreter, which prints a welcome banner showing the version of Python, then a >>> prompt.

You then typed print("Hello World") at the prompt. All good so far.

Now the mistake, but an understandable one.

You used the IDLE “Save” or “Save As” command to save that to a file. But unfortunately, IDLE doesn’t just save your code, it also saves the banner (“Python 3.10.3 …”) to the file.

Which is, I think, a bug in IDLE:

https://bugs.python.org/issue45297

Until this is fixed in IDLE, which may not happen for a while, the best way to use IDLE to save your code is to always use the Open command and check the script, removing any of the unwanted junk lines added to the file.

1 Like

Thanks.