Forums

f strings compilation issue

I am brand new to pythonanywhere. I have my code working on my local dev machine and trying to get it working here.

When I run (returntag-virtualenv) 17:06 ~ $ python -i /var/www/returntag_eu_pythonanywhere_com_wsgi.py to check why my site is not working, I get the following error.

Traceback (most recent call last):File "/var/www/returntag_eu_pythonanywhere_com_wsgi.py", line 78, in <module>
from main import app as application

File "/home/returntag/main.py", line 16, in <module>
app = create_app()

File "/home/returntag/website/__init__.py", line 60, in create_app
from .product_info import product_info File "/home/returntag/website/product_info.py", line 375
flash(f'Another {session**['cart_item'**][key]['name']} has been added to your Subscription cart.', category="success")
                                            ^^^^^^^^^

SyntaxError: f-string: unmatched '['

I did have the error in another part of my code where I was accessing a configuration variable which it didn't like so I hard coded that as a test . It was

log.debug(f'Account_id_to_check {account_id_to_check},  last scan was less than {current_app.config['PAYG_HOURS_BETWEEN_SCANS']} hours ago')

changed to

log.debug(f'Account_id_to_check {account_id_to_check},  last scan was less than 24 hours ago')

and after the change it was no longer flagged by this compiler but now getting the error below on the flash

flash(f'Another {**session['cart_item'][key]['name']**} has been added to your Subscription cart.', category="success")

The log files show (before I made the change stated above)

2024-03-06 17:10:45,553: Error running WSGI application 2024-03-06 17:10:45,560: File "/home/returntag/./website/commonFunctions.py", line 154 2024-03-06 17:10:45,560: 2024-03-06 17:10:45,560: log.debug(f'Account_id_to_check {account_id_to_check}, last scan was less than {current_app.config['PAYG_HOURS_BETWEEN_SCANS']} hours ago') 2024-03-06 17:10:45,560: 2024-03-06 17:10:45,560: ^^^^^^^^^^^^^^^^^^^^^^^^ 2024-03-06 17:10:45,560: 2024-03-06 17:10:45,560: SyntaxError: f-string: unmatched '[' 2024-03-06 17:10:45,561: File "/var/www/returntag_eu_pythonanywhere_com_wsgi.py", line 78, in <module> 2024-03-06 17:10:45,561: from main import app as application 2024-03-06 17:10:45,561: 2024-03-06 17:10:45,561: File "/home/returntag/./main.py", line 16, in <module> 2024-03-06 17:10:45,561: app = create_app() 2024-03-06 17:10:45,561: 2024-03-06 17:10:45,561: File "/home/returntag/./website/init.py", line 57, in create_app 2024-03-06 17:10:45,562: from .dashboard import dashboard 2024-03-06 17:10:45,562: 2024-03-06 17:10:45,562: File "/home/returntag/./website/dashboard.py", line 20, in <module> 2024-03-06 17:10:45,562: from .commonFunctions import checkInputs, is_float,checkPersonID, allowed_image, uniqueEmail, emailFormatOk,get_random_code, SendMail, useful 2024-03-06 17:10:45,562: ******* 2024-03-06 17:10:45,562: If you're seeing an import error and don't know why, 2024-03-06 17:10:45,562: we have a dedicated help page to help you debug: 2024-03-06 17:10:45,562: https://help.pythonanywhere.com/pages/DebuggingImportError/ 2024-03-06 17:10:45,562: *******

I should have mentioned, I am running python 3.10 and have a virtual environment which I have double checked is on 3.10 as well

(returntag-virtualenv) 17:09 ~ $ python --version Python 3.10.5

You're using the same quotes (single quotes) in the f-string as you're using to wrap the fstring. So, in effect, you are closing the f-string early. Make sure that the quotes that you use to wrap the f-string (either single or double) are different to the ones that you use inside that string or that you escape the quotes that are inside the string.

Thanks glenn, I'll give that a try and feedback here

Thanks glenn. that worked a charm. On to the next error :)

Glad we could help with this one!