invalid syntax running python manage.py migrate - python

After I enter:
python manage.py migrate
I get an error on this line:
archive_files = s3.list_objects_v2(Bucket=settings.ARCHIVE_BUCKET, Prefix=f"{org.id}/")["Contents"]
^
I don't understand why django 2.0.3 is complaining.

It looks like you're using format strings with python 3.5, however support for formatted string literals was not added until python 3.6. More information can be found in the python documentation. You will either need to update your python version or format the string another way. e.g. "{}/".format(org.id)

In the traceback we see that the caret points to a string interpolation [PEP-0498]. This feature is supported since python-3.6, but you run your code with:
python3.5 manage.py runserver
so the interpreter does not understand this string interpolation.
You basically have two options:
try to run this with python-3.6 (or higher), although there is of course always a risk that something is broken then:
python3.6 manage.py runserver
perform proper formatting, replace:
prefix=f"{org.id}/"
with:
prefix="{}/".format(org.id)
or some other way to format this.

Related

Invalid Syntax in F string Python 3 [duplicate]

I tried this code, following along with a tutorial:
my_name = 'Zed A. Shaw'
print(f"Let's talk about {my_name}.")
But I get an error message highlighting the last line, like so:
print(f"Let's talk about {my_name}.")
^
SyntaxError: invalid syntax
Why?
If you get an error like this from someone else's code, do not try to fix it yourself - the other project simply does not support your Python version (One example: Using pytesseract on Python 2.7 and Windows XP). Look for an alternative instead, or check the project's documentation or other support resources for a workaround.
In particular, the built-in pip package manager has an issue where newer versions of pip require a newer Python version, so old installations cannot upgrade pip past a certain point. See Installing pip is not working in python < 3.6 or Upgrading pip fails with syntax error caused by sys.stderr.write(f"ERROR: {exc}") for details.
If an external tool warns about the problem even though Python supports the feature, update the tool. See Getting invalid syntax error when using Pylint but code runs fine for an example.
This question is specifically about the situation where any attempt to use f-strings fails (the ^ in the error message will point at the closing quote). For common problems with specific f-string syntax, see How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?, f-string formula inside curly brackets not working, Discord.py - SyntaxError f-string: empty expression not allowed, How to use newline '\n' in f-string to format output in Python 3.6?.
For details on alternate approaches to string formatting, see How do I put a variable’s value inside a string (interpolate it into the string)?.
I think you have an old version of python. try upgrading to the latest version of python. F-string literals have been added to python since python 3.6. you can check more about it here
This is a python version problem.
Instead of using
print(f"Let's talk about {my_name}."
use
print("Let's talk about {}.".format(my_name))
in python2.
Your code works on python3.7.
Check it out here:
my_name= "raushan"
print(f"Let's talk about {my_name}.")
https://repl.it/languages/python3
Python Interpreter causes the following issue because of the wrong python version you calling when executing the program as f strings are part of python 3 and not python 2. You could do this python3 filename.py, it should work. To fix this issue, change the python interpreter from 2 to 3. 
f-strings were added in python 3.6. In older python versions, an f-string will result in a syntax error.
If you don't want to (or can't) upgrade, see How do I put a variable inside a String in Python? for alternatives to f-strings.
I think this is due to the old version. I have tried in the new version and the executing fine. and the result is as expected.
I believe the problem you are having here is down to you using python 2 without realizing it. if you haven't set it up on your machine to have python 3 as your default version you should execute python3 in your terminal instead of the standard 'python' command.
I had this problem so hopefully, this answer can be of help to those looking for it.
I think they had typed
python file.py
to run the program in the Mac or linux that runs the python 2 version directly because OS defaultly contain python 2 version, so we needed to type
python3 file.py
That's the solution for the problem
python2 and python3 running command

Python3 print(f" ...") issue with format method [duplicate]

I tried this code, following along with a tutorial:
my_name = 'Zed A. Shaw'
print(f"Let's talk about {my_name}.")
But I get an error message highlighting the last line, like so:
print(f"Let's talk about {my_name}.")
^
SyntaxError: invalid syntax
Why?
If you get an error like this from someone else's code, do not try to fix it yourself - the other project simply does not support your Python version (One example: Using pytesseract on Python 2.7 and Windows XP). Look for an alternative instead, or check the project's documentation or other support resources for a workaround.
In particular, the built-in pip package manager has an issue where newer versions of pip require a newer Python version, so old installations cannot upgrade pip past a certain point. See Installing pip is not working in python < 3.6 or Upgrading pip fails with syntax error caused by sys.stderr.write(f"ERROR: {exc}") for details.
If an external tool warns about the problem even though Python supports the feature, update the tool. See Getting invalid syntax error when using Pylint but code runs fine for an example.
This question is specifically about the situation where any attempt to use f-strings fails (the ^ in the error message will point at the closing quote). For common problems with specific f-string syntax, see How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?, f-string formula inside curly brackets not working, Discord.py - SyntaxError f-string: empty expression not allowed, How to use newline '\n' in f-string to format output in Python 3.6?.
For details on alternate approaches to string formatting, see How do I put a variable’s value inside a string (interpolate it into the string)?.
I think you have an old version of python. try upgrading to the latest version of python. F-string literals have been added to python since python 3.6. you can check more about it here
This is a python version problem.
Instead of using
print(f"Let's talk about {my_name}."
use
print("Let's talk about {}.".format(my_name))
in python2.
Your code works on python3.7.
Check it out here:
my_name= "raushan"
print(f"Let's talk about {my_name}.")
https://repl.it/languages/python3
Python Interpreter causes the following issue because of the wrong python version you calling when executing the program as f strings are part of python 3 and not python 2. You could do this python3 filename.py, it should work. To fix this issue, change the python interpreter from 2 to 3. 
f-strings were added in python 3.6. In older python versions, an f-string will result in a syntax error.
If you don't want to (or can't) upgrade, see How do I put a variable inside a String in Python? for alternatives to f-strings.
I think this is due to the old version. I have tried in the new version and the executing fine. and the result is as expected.
I believe the problem you are having here is down to you using python 2 without realizing it. if you haven't set it up on your machine to have python 3 as your default version you should execute python3 in your terminal instead of the standard 'python' command.
I had this problem so hopefully, this answer can be of help to those looking for it.
I think they had typed
python file.py
to run the program in the Mac or linux that runs the python 2 version directly because OS defaultly contain python 2 version, so we needed to type
python3 file.py
That's the solution for the problem
python2 and python3 running command

Django - Unqualifed exec not allowed in function 'namedtuple'

So, I'm using Django with Python 2.7. I've just tried running python setup.py install, and am getting an error: Unqualifed exec not allowed in function namedtuple.
The source of setup.py is huge, so is in a pastebin here: http://pastebin.com/P2uJMG9n
Does anybody have any idea of why it won't work?
Thanks,
Connor

Error using gnuplot from within Sage, but works fine in regular Python

I apologize if this isn't the best place to ask this question, but hopefully someone here can help. I want to run some gnuplot commands directly from within a Sage script, but I get the following error message:
dyld: Library not loaded: /opt/local/lib/libfreetype.6.dylib
Referenced from: /opt/local/bin/gnuplot
Reason: Incompatible library version: gnuplot requires version 14.0.0 or later, but libfreetype.6.dylib provides version 10.0.0
This message appears if I try to use the gnuplotpy interface in Sage, or if I just use something like os.system("gnuplot -e \"plot('sin(x)')\"") from Sage. However, the same os.system(...) command works just fine in regular python. Many thanks.
Sage changes a number of environments including PATH, LD_LIBRARY_PATH, etc. This can cause problems running binaries not installed inside Sage. For this reason it provides a shell command sage-native-execute which (mostly) changes all the variables back. So try the following—it fixes the problem for me:
os.system('''sage-native-execute gnuplot -e "plot('sin(x)')"''')

Syntax Error in Python Request Library when Using on My Test server

I'm using Python Request Library in one of my projects. Everything works fine on my local system where I have Python 2.6 and Django 1.2.3.
I have created the same environment for my Test server(Python 2.6 and Django 1.2.3 and is using WSGI 3.2). Now I get some weird syntax errors. For example:
line 266
result.append((k.encode('utf-8') if isinstance(k, unicode) else k,
^
SyntaxError: invalid syntax*
Here's another:
from .config import settings
^
SyntaxError: invalid syntax*
I'm guessing it's something because of wsgi but not sure exactly. Can someone please help me to solve this issue.
Thanks
This errors mean that actually you are not using Python 2.6. Probably Python 2.4 is also installed on you environment and it is used to execute your code.
I'm guessing your webserver (such as apache) is using an older version of python, rather than the python 2.6 environment you setup.

Categories