flake8 xxx --ignore=E501,E128,E701,E261,E301,E126,E127,E131
xxx.py:1:40: E901 SyntaxError: invalid syntax
Any one has any idea where is the syntax error?
Python==2.6, first line of the file is, no byte order marking :
from __future__ import absolute_import
Works fine in Python 2.7 and 3+ though.
If you add --show-source to the flake8 command it'll point out the error in the output.
A bit hard to guess without complete file, ideally in some format that preserves bytes exactly.
:1:40 refers to first line, char position 40. The line is 38 characters long.
Thus suspicion falls on newline marker.
Most likely newline (single char) is not recognised and Python (not flake8 btw) treats this line and the next and one long line. Thus error is in column 40.
Alternatively your newline is a sequence of 2 chars and 2nd char is not understood correctly.
There could theoretically also be an encoding problem, but I find that quite unlikely.
Related
OS Windows 10 Pro
Versions of xlwings, Excel, and Python (0.9.0, Office 365, Python 3.8.2)
I am new on using xlwings through VBA. I run the exact syntax from a tutorial webpage on both VBA and Python, but it gives error like this:
File "<string>", line 1
import sys, os; sys.path[0:0]=os.path.normcase(os.path.expandvars(r'C:\Users\User\Trial2;C:\Users\User\Trial2\Trial2.zip;C:\Users\User\Anaconda3\')).split(';'); import Trial2;Trial2.main()
SyntaxError: invalid syntax
I used original syntax for VBA, and the syntax I used for python is like this:
import xlwings as xw
##xw.sub # only required if you want to import it or run it via UDF Server
def main():
wb = xw.Book.caller()
wb.sheets[0].range("A1").value = "Hello xlwings!"
##xw.func
def hello(name):
return "hello {0}".format(name)
if __name__ == "__main__":
xw.Book("Trial2.xlsm").set_mock_caller()
main()
I barely find any clue for this problem, so I'm hoping that someone can give me a solution
I realize this is a long time after the initial question but I had the same issue and couldn't find an answer anywhere. After playing around (for much longer than I care to admit) I found the problem for me was that my .xlsm/.py file names contained a space. With no other changes, everything worked when I replaced the space with an underscore.
This is a quirk in python's string literals. Even with raw strings the backslash escapes the quote character so r"ends in quote\"" is valid. It also means that raw strings can't end in a single backslash. r"ends in slash\" is a syntax error. If you need to end a string with a backslash, you can't use raw. "ends in slash\\" is okay.
I'm not sure where the failing string comes from, but you need to change it to
import sys, os; sys.path[0:0]=os.path.normcase(os.path.expandvars('C:\\Users\\User\\Trial2;C:\\Users\\User\\Trial2\\Trial2.zip;C:\\Users\\User\\Anaconda3\\')).split(';'); import Trial2;Trial2.main()
See Python Lexical Analysis
Even in a raw literal, quotes can be escaped with a backslash, but the backslash remains in the result; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw literal cannot end in a single backslash (since the backslash would escape the following quote character).
I am making a Python project that needs to work with Greek characters print, edit and return strings.
On my main PC that has the Greek language installed everything runs fine but when I am running on my English laptop the same program with the same version of python an encode error is triggered. Especially this one:
EncodeError: 'ascii' codec can't encode characters in position 0-2:
ordinal not in range(128)
The error happens due to this code
my_string = "Δίας"
print(my_string)
Why is this happening and what I need to do to fix it?
Why is this happening? You are using Python 2 and although it supports Unicode, it makes you jump through a few more hoops explicitly than Python 3 does. The string you provide contains characters that fall outside the normal first 128 ASCII characters, which is what is causing the problem.
The print statement tries to encode the string as standard ascii, but it runs into characters it doesn't understand and by that point, it does not know what encoding the characters are supposed to be in. You might think this is obvious: "the same encoding the file is in!" or "always UTF-8!", but Python 2 wants you to make it explicit.
What do you need to do to fix it? One solution would be to use Python 3 and not worry about it, if all you need is a quick solution. Python 3 really is the way forward at this point and using Python 2 makes you solve problems that many Python programmers today don't have to solve (although they should be able to, in the end).
If you want to keep using Python 2, you should change your code to this:
# coding=utf-8
my_string = u"Δίας"
print(my_string.encode('utf-8'))
The first line tells the interpreter explicitly what encoding the source file was written in. This helps your IDE as well, to make sure it is showing you the code correctly. The second line has the u in front of the string, telling Python my_string is in fact a unicode string. And the third line explicitly tells Python that you want the output to be utf-8 encoded as well.
A more complete explanation of all this is here https://docs.python.org/2/howto/unicode.html
If you're wondering why it works on your Greek computer, but not on your English computer - the default encoding on the Greek computer actually has the code points for the characters you're using, while the English encoding does not. This indicates that Python is clever enough to figure out that things are utf (and the string is a series of unicode code points), but by the time it needs to encode them, it doesn't know what encoding to use, as the standard (English) encoding doesn't have the characters in the string.
I'm trying to figure out how to compile a github project with Python. I imported my os, but I am getting a syntax error when I attempt to change directories with this code: os.chdir(C:\Users\User\Desktop\Folder)
After doing that, I get this:
>>> os.chdir(C:\Users\User\Desktop\Folder)
File "<stdin>", line 1
os.chdir(C:\Users\User\Desktop\Folder)
^
SyntaxError: invalid syntax
I see that it is pointing at the colon. Am I putting in the directory incorrectly? (I have never used python in my life.) Any help would be greatly appreciated. Thanks in advance!
You need to pass it a string. And because it's a Windows path, it should be a raw string (Quote mark prefixed with r, like r''), so the backslashes don't get interpreted as string literal escapes (raw strings are more succinct than the alternative of doubling all the backslashes), making it:
os.chdir(r'C:\Users\User\Desktop\Folder')
I am new to working with Python, and seem to be having an issue that no matter what I search, and no matter what I try, still presists.
I am trying to execute a system command using the exec function.
I have tried the following:
exec("/usr/sbin/something --arg")
Which returns invalid syntax with a cursor on the first /
I have tried
exec("\\usr\\sbin\\something --arg")
Which returns unexpected character after line continuation character and a cursor on the last character of my argument g in this case.
I have been searching for a solution for the last hour, and nothing has helped. How can I simply execute this command without an error being thrown?
Yes, my line endings are correct.
exec is used to execute Python code that is represented as a string or code object. From the docs:
exec(object[, globals[, locals]])
This function supports dynamic execution of Python code. object must be either a string or a code object. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs).
To execute a system command, you can use os.system:
import os
os.system("/usr/sbin/something --arg")
Maybe try putting an r in front of the string so it reads exec(r"\\usr\\sbin\\something --arg")
I have a feature of my program where the user can upload a csv file, which my program goes through and uses as input. I have one user complaining about a problem where his input is throwing up an error. The error is caused by there being an illegal character that is encoded wrong. The characters is below:
�
Sometimes it appears as a diamond with a "?" in the middle, sometimes it appears as a double diamond with "?" in the middle, sometimes it appears as "\xa0", and sometimes it appears as "\xa0\xa0".
In my program if I do:
print str_with_weird_char
The string will show up in my terminal with the diamond "?" in place of the weird character. If I copy+paste that string into ipython, it will exit with this message:
In [1]: g="blah��blah"
WARNING:
********
You or a %run:ed script called sys.stdin.close() or sys.stdout.close()!
Exiting IPython!
notice how the diamond "?" is double now. For some reason copy+paste makes it double...
In the django traceback page, it looks like this:
UnicodeDecodeError at /chris/import.html
('ascii', 'blah \xa0 BLAH', 14, 15, 'ordinal not in range(128)')
The thing that messes me up is that I can't do anything with this string without it throwing an exception. I tried unicode(), I tried str(), I tried .encode(), I tried .encode("utf-8"), no matter what it throws up an error.
What can I do it get this thing to be a working string?
You can pass, "ignore" to skip invalid characters in .encode/.decode
like "ILLEGAL".decode("utf8","ignore")
>>> "ILLEGA\xa0L".decode("utf8")
...
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa0 in position 6: unexpected code byte
>>> "ILLEGA\xa0L".decode("utf8","ignore")
u'ILLEGAL'
>>>
Declare the coding on the second line of your script. It really has to be second. Like
#!/usr/bin/python
# coding=utf-8
This might be enough to solve your problem all by itself. If not, see str.encode('utf-8') and str.decode('utf-8').
you can also use:
python3 -c "import urllib, sys ; print urllib.quote_plus(sys.stdin.read())";
taken from https://wiki.python.org/moin/Powerful%20Python%20One-Liners
** ps, in the website it's pointed to use python, but I tested in python3 and it works just fine
The only way to do it (at least in python2) is to use unicodedata.normalize:
unicodedata.normalize('NFKD', text).encode('utf-8', 'ignore')
decode('utf-8', 'ignore') will just raise exception.