31

When doing python3 Webhook.py (this is the file), it gives me the error: File "<fstring>", line 1 (%X - %x) ^ SyntaxError: invalid syntax

I've tried to print out the raw contents of the file and I also used a hex editor, there is nothing on line 1 that should be causing erorrs. I also did: import time, os, aiohttp, plistlib, discord, asyncio, json, subprocess In the Terminal.app version of Python3 and I had no errors, my version was 3.6.3 but updated to 3.6.5 to check if the issue would go away, which didn't. Can anyone help?

incredaboy
  • 364
  • 1
  • 4
  • 12
  • 5
    For anyone looking at this after the fact: when I received the same error, it turned out that I had an f string syntax error further down in the file even though the error appeared to refer to the import line. Once I resolved this problem, I stopped receiving the error. – Daniel Sep 21 '18 at 18:09
  • Resolved in Python 3.8 –  Nov 16 '21 at 08:31

6 Answers6

33

I had a similar issue. After removing parts of the code to narrow down the problem, I found the root cause.

In my case I was printing an f-string and inside the f-string I had a space in the name of the variable I was calling

var32 = x
print(f"This is a statement {var 23} "

Removing that space fixed the problem.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Ignacio
  • 431
  • 4
  • 4
28

it might also be that you have a python 3.8 compatible way of string formating, such as:

f"print{count=}"

which is not compatible with 3.6, 3.7.

In that case, you can either decide to require the user to upgrade to python3.8, or downgrade your code.

Ben Lin
  • 807
  • 10
  • 15
11

This happens when anything inside {} is not valid, in a string formatted using the f string formatting prefix. Python 3.7 in my case. The upside is you get the string causing the problem on the error message. You don't get the line number, but it's still easy to figure out once you acknowledge that line number 1 is not the correct line number of the error.

matanster
  • 15,072
  • 19
  • 88
  • 167
0

for my code. the problem was

I was printing {=*10} instead the right form {"="*10} that mistake generated my problem

Ak_imn
  • 1
0

I pass the string in the like print(f"The length of the set is {len(s) elements}")

So I just move my string out of and problem solved. print(f"The length of the set is {len(s)} elements")

0

This happened to me. What solved it for me is that I put f before a string for example print(f'string {hello}') and had the curly brackets in my string, which were meant to be a string and not a variable due to the f before the string. Hopefully, this helped for you.

ouflak
  • 2,458
  • 10
  • 44
  • 49