Error in python asterisk syntax. No idea what is wrong - python

I'm trying to locally minimize a complicated function using scipy.optimize.minimize. Since I require good gradients in order for the local optimization to be smoothily performed, but the function is so very much complicated in order for the derivatives to be written by hand, I decided to use Autoptim as the middle-man to handle my optimization using the automatic differentiation package Autograd to obtain the gradients.
After I installed the package (as well as Autograd), I opened my python terminal in order to run a few preliminary tests to check whether the installation and the package integration between scipy, autograd and autoptim went smoothily. Then Autoptim raised an error immediately upon import (at the line import autoptim). Since the interpreter gives the full stack of Exceptions raised, I went to the deeper layers to see what line initiated the cascade that halted the interpreter.
The line I found was line 88 of autoptim.py:
87. optim_vars = _convert_to_tuple(optim_vars)
88. precon_optim_vars = precon_fwd(*optim_vars,*args)
89. n_args = len(args)
Python interpreter raised an Invalid Syntax Exception, which means that something in that line is not written "in Python". I checked to see whether there was some unclosed parenthesis and that was not the case. I am using Python 3 so I figured that maybe something on that line was written in Python 2 syntax and it registers wrong for a Python 3 interpreter but as I understand it, the differences between the two versions is quite small and there is some (although not complete) retrocompatibility between the two.
So what gives? What am I missing here?
What is wrong with that line?
Here is the traceback of the import line in the python interpreter
>>> import autoptim
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/autoptim/__init__.py", line 6, in <module>
from .autoptim import minimize # noqa
File "/usr/local/lib/python3.4/dist-packages/autoptim/autoptim.py", line 95
return objective_function(*optim_vars, *args)
^
SyntaxError: invalid syntax

The syntax being used wasn't introduced until Python 3.5 (see PEP 448). You are using Python 3.4.
As a workaround, you could explicitly build the required list to unpack:
return objective_function(*list(optim_vars + args))

Related

Why does "import pyautogui" give an error [duplicate]

This question already has an answer here:
Why is pyauto gui and Pycharm not working for me?
(1 answer)
Closed last year.
I dont understand why this gives back an error and i'm really confused.
>>> import pyautogui
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/connorgooch/Library/Python/2.7/lib/python/site-packages/pyautogui/__init__.py", line 249, in <module>
import mouseinfo
File "/Library/Python/2.7/site-packages/mouseinfo/__init__.py", line 100, in <module>
from rubicon.objc import ObjCClass, CGPoint
File "/Library/Python/2.7/site-packages/rubicon/objc/__init__.py", line 3, in <module>
from .runtime import ( # noqa: F401
File "/Library/Python/2.7/site-packages/rubicon/objc/runtime.py", line 785
self.restype, *self.argtypes = ctypes_for_method_encoding(self.encoding)
^
SyntaxError: invalid syntax
How do I fix this?
Diagnosis
You're running the library in a version of Python that's too old.
You can tell because assignment of the form ..., *whatever = some_function() became valid only in a relatively recent version of Python.
Prior to that, having a unary * (splat operator) like that on the left side of an assignment was a syntax error.
More specifically, you're using Python 2, which we can tell by both the fact that this feature became available in Python 3.0, and the fact that the traceback has paths like this: /Library/Python/2.7/... (that 2.7 is the Python version).
Solution
Use a newer version of Python.
I can't tell you how to do that because that totally depends on how you're currently running Python.
On some systems, the python command is still Python 2, and you need to call python3 to get Python 3. You might also have a situation where you have separate commands like python27 for Python 2.7, python38 for Python 3.8, and python39 for Python 3.9, but python3 and python point to Python 3.8 instead of either the latest 2 or the latest 3.
Also, on some systems, each Python version is installed in a different directory, and if you specify the right path you don't need to worry about the exact command name.
So if you're calling Python from a shell, you may need to change the PATH or the command or both. If you're calling Python from an IDE, there might be a setting for it. And so on.
Similarly, you might need to actually install a newer version of Python, but the right way to do that depends on your system.
Maybe the package manager provided by your operating system like apt or yum or any number of others, or a third-party package manager like brew or chocolatey, or maybe you're using a tool that manages multiple Python versions. Or maybe you just download it manually from the Python webpage.
Based on your traceback, the fact that some of the paths start with /Library/... suggests that you're on MacOS, which means you can probably just get Python 3 by using the command python3, or changing the path your tool is using to Library/Python/3.? (replace ? with the right minor version). And if you don't already have it installed, you can get it from the Python website or the "homebrew" package manager if you're using that or whatever else works on Mac.
Solution (if you really can't use a newer Python)
You can change the line that's raising the syntax error.
There are many equivalent ways to do this once you understand what's going on.
Here is one pattern which is less readable than most, but which you can use in all cases with no understanding or review or changes:
self.restype, self.argtypes = (lambda r: (r[0], list(r[1:])))(ctypes_for_method_encoding(self.encoding))
Background
In Python, a function can return a tuple or something similar like a list.
This is how returning multiple values is implemented.
In order to make this work on the assignment side, Python can do something called "iterable unpacking": foo, bar = my_function() (this is more generally known by other names too, such as "de-structuring assignment").
(This is also what's happening when you "swap" values: foo, bar = bar, foo is actually semantically equivalent to temp = (bar, foo); foo = temp[0]; bar = temp[1]; del temp. First a tuple is created, and then it goes through de-structuring assignment.)
In Python 3, they added a feature where an assignment with the "splat" operator (*) on one of the values will capture "the rest" of the de-structured values. So if you do temp = (1, 2, 3, 4, 5), then foo, bar, *qux = temp will result in foo = 1, bar = 2, and qux = (3, 4, 5).
But in earlier versions of Python that feature didn't exist.
So the line
self.restype, *self.argtypes = ctypes_for_method_encoding(self.encoding)
is taking the return value from ctypes_for_method_encoding, and assigning the first element of it into self.restype and a tuple holding the rest of the elements from it into self.argtypes.
First try:-
python -m pip install --upgrade pyautogui
If it does not work, then in File "/Library/Python/2.7/site-packages/rubicon/objc/runtime.py", line 785
self.restype, *self.argtypes = ctypes_for_method_encoding(self.encoding)
Try correcting the syntax because you have a syntax error in this line. You can find the syntax on google.
I think you have an error in the asterisk symbol *. But I am not sure.

Using osmviz in Python

I am trying to use the osmviz module of Python, which allows to use maps from OpenStreetMap.
So I downloaded it with pip, and then I tried to run one of the examples offered by the documentation page of osmviz (https://hugovk.github.io/osmviz/html/doc.html), the third one (https://hugovk.github.io/osmviz/html/pil_example.py.html).
However it doesn't work, I keep getting the following error :
Traceback (most recent call last):
File "testosm.py", line 1, in <module>
from osmviz.manager import PILImageManager, OSMManager
File "/home/FUNDP/mcohilis/.local/lib/python3.4/site-packages/osmviz/manager.py", line 60
raise Exception, "UNIMPLEMENTED"
^
SyntaxError: invalid syntax
So the error seems to be inside the module code and is a syntax error, which I find very weird. What could I do about it?
I get the same error with another code using osmviz, and I tried with two different computers, it doesn't change anything.
Does someone know how to use osmviz ?
Thanks a lot,
Marie

Code not compiling in Python3

I wrote a code and it is getting compiled on my PC with Python3. But Showing error while uploading to Codechef server. Please suggest, I am coding for the first time in Python 3.
Traceback (most recent call last):
File "/run-ls7W2DcLmzUs9GNKbLGN/solution.py", line 41, in <module>
l,r,k=map(int,input().split())
File "<string>", line 1
9 23 1
^
SyntaxError: invalid syntax
You're using python2.x which evaluates the string you enter for input. Change the function from input to raw_input and you should be all set.
If you want the code to work for both python2.x and python3.x, you could do a simple little hack like this at the top of your script:
try:
#This raises `NameError` on python3.x since `raw_input` is renamed to `input`
input = raw_input
except NameError:
pass
It's not pretty, but it works (and I've used things like this on occasion). Ultimately, this shadows the builtin input on python2.x, but that's really not a big deal. You probably don't want to be using that builtin for any serious coding anyway.

Using Ladon in Python 2.6 on windows

I have been trying to create a web service out some python scripts, and haven't seemed to have had much luck. I am new to web services in general, but would really like to get this figured out. I'm on Windows 7 and use IIS7. The service also needs to be SOAP.
I've read through most posts that have anything to do with python and SOAP and tried out pretty much all the different libraries, but most of them just seem over my head (especially ZSI/SOAPpy). The Ladon Library seems like it would be best (and simplest) for what I need, but the tutorial http://www.youtube.com/watch?v=D_JYjEBedk4&feature=feedlik loses me at 5:10 when he brings it to the server. When I type the ladon2.6ctl in the cmd, it seems like windows gets quite confused. I'm guessing it is a little different because he is running on Linux and using Apache.
With that, any tips on how to get a python web service running on Microsoft 'stuff' would be greatly appreciated, as I have been trying to figure this stuff out for way too long.
One thing to note is the reason things are so specific (and seemingly strange) is because the scripts I have do a lot of geoprocessing with ESRI's "arcpy".
--Addition--
Traceback on localhost:8080/TestService:
Traceback (most recent call last):
<br>File "c:\Python26\ArcGIS10.0\lib\site-packages\ladon-0.5.1-py2.6.egg\ladon\server\wsgi_application.py", line 229, in __call__
exec("import %s" % ','.join(self.service_list))
File "<string>", line 1, in <module>
File "c:\Users\r\workspace\ladon\src\testspace.py", line 3, in <module>
class TestService2(object):
File "c:\Users\r\workspace\ladon\src\testspace.py", line 4, in TestService2
#ladonize(int,int,rtype=int)
File "c:\Python26\ArcGIS10.0\lib\site-packages\ladon-0.5.1-py2.6.egg\ladon\ladonizer\decorator.py", line 87, in decorator
ladon_method_info = global_service_collection().add_service_method(f,*def_args,**def_kw)
File "c:\Python26\ArcGIS10.0\lib\site-packages\ladon-0.5.1-py2.6.egg\ladon\ladonizer\collection.py", line 119, in add_service_method
sinfo = self.source_info(src_fname)
File "c:\Python26\ArcGIS10.0\lib\site-packages\ladon-0.5.1-py2.6.egg\ladon\ladonizer\collection.py", line 79, in source_info
a = ast.parse(src)
File "c:\Python26\ArcGIS10.0\lib\ast.py", line 37, in parse
return compile(expr, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 1
from ladon.ladonizer import ladonize
^
SyntaxError: invalid syntax
sample code:
from ladon.ladonizer import ladonize
class TestService2(object):
#ladonize(int,int,rtype=int)
def sum(self,a,b):
'''add two numbers<br>
param a: number 1
param b: number 2
rtype: sum of result
'''
return a+b
I must admit I normally use Linux for almost everything and I haven't tried Ladon on Windows for a while. I will spin up my windows installation later today and see if there is any trouble.
You wrote that ladon2.6ctl get's confused. Do you have an exception Traceback?
To summarize the fix for anyone else interested, delete the "syslog import" from these 3 ladon modules: ladon/interfaces/jsonwsp.py - line 6
ladon/dispatcher/dispatcher.py - line 7
ladon/server/wsgi_application.py - line 37
Then, you need to change the linefeed from window's default of /r/n to /n. In Eclipse, go to Window -> Preferences -> General, then select (not drop down) the Workspace tab. On the bottom right, select "other" under New text file line delimiter and change it to Unix.

There is no spawnl function in python 2.6?

I just noticed that my old codes written in python 2.5 does not work now. I am in python 2.6 btw.
>>> os.spawnl(os.P_NOWAIT,"setup.exe")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\python26\lib\os.py", line 612, in spawnl
return spawnv(mode, file, args)
OSError: [Errno 22] Invalid argument
>>>
Any clue? or do you have any working sample of os.spawn* with NOWAIT option.
Update:
Even I put full path in os.spawnl(), Its still error.
thrope is right about subprocess being preferred. But the spawn* stuff is still there in 2.6. In fact, you can see that in your error message. Your first arg seems to be valid. I'd check the second arg, which is the path.
I got it work by adding DUMMY parameter finally, a bit funky though
This is not working
os.spawnl(os.P_NOWAIT,"Setup.exe")
This is also not working
os.spawnl(os.P_NOWAIT,"Setup.exe","")
But this is working
os.spawnl(os.P_NOWAIT,"Setup.exe","DUMMY")
Thanks all anyway.
I think its recommended to use the subprocess module these days rather than the os.spawn* functions. (I can't reproduce your problem, but I'm not on windows).
A Google search brings up this page about the same problem happening when there is a space in the Python installation path. I couldn't reproduce it here, but maybe it's the problem?
In any case, according to MS documentation this error value (EINVAL) should only be returned if the mode argument is invalid, which isn't the case here.
os.spawnl() requires full path to executable, while os.spawnlp() uses PATH environment variable to find it.
Update: Also it's common error to use unescaped backslashes in the path literal (try printing it to see whether it's interpreted right).

Categories

Resources