So I am trying to use python's wifi module to get neary wireless networks and log into them using a password. So far this is my code just to get the networks...
from wifi import Cell, Scheme
Cell.all('interfacename')
And I am receiving the following error...
Traceback (most recent call last):
File "python", line 1, in <module>
File "/goval_modules/python3.6/pbkdf2/__init__.py", line 69
print pbkdf2(p, s, l, i).encode('base64')
^
SyntaxError: invalid syntax
Can someone please help me understand why this error is appearing and how to fix it?
As Kanak mentioned, this WiFi module was written in python2, but you are using it in python3. Unfortunately, there is no workaround for this.
One possible fix could be trying to find an updated version of this module. You could also write your code in python2, but that might be a bit impractical.
Related
I'm trying to get the ODB library working. In the documentation at https://python-obd.readthedocs.io/en/latest/ it lists the following code:
import obd
connection = obd.OBD("/dev/ttyUSB0") # connects to USB or RF port
cmd = obd.commands.SPEED # select an OBD command (sensor)
response = connection.query(cmd) # send the command, and parse the response
print(response.value) # returns unit-bearing values thanks to Pint
print(response.value.to("mph")) # user-friendly unit conversions
When I put this in a file called test.py and I run it:
python2 test.py
I get the following error message:
Traceback (most recent call last):
File "test.py", line 1, in <module>
import obd
File "/home/ubuntu/obd.py", line 3, in <module>
AttributeError: 'module' object has no attribute 'OBD'
Stackoverflow comes up with several iterations of this error message, but none clearly explain the problem, only giving specific solutions to those libraries.
I guess it's obvious that I'm new to Python, and I'm having trouble interpreting this error message, even after several hours of writing several small Python programs. I am of course also interested in why the error message is so un-intuitive to a newcomer and where I can gain the common knowledge I might be missing, and I guess this is as good a case to discover that through as any.
So far I have figured out the following:
<module> refers to the name of my python script that I am trying to run.
The mistake I made at first, was to name my test.py file, initially obd.py. This conflicted with the import obd as it was trying to import the file itself - even after I deleted it! The reason was that when I tried to run it the first time, it created a file called obd.pyc - and even though obd.py was no longer there, import OBD found the initially created obd.pyc and tried to import that - which of course does not contain the OBD object from the library I was trying to use.
This is not the detailed answer I'm looking for, so please add a more detailed explanation - or a link to how Python compiling works, if you can.
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
I am wondering if it is possible to edit/customize the behavior and printout of built-in errors in Python. For example, if I type:
>>> a = 1
>>> print A
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'A' is not defined
I want the output to instead be:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'A' is not defined. Check capitalization.
Moreover, I want this to occur at a global level, for ALL FUTURE CODE, without having to explicitly include an exception in my code. If such a change is possible, I would assume this needs to be done at the very source or library-file level of Python. However, I am not sure where exactly to look to know if this is even possible.
I am using Python 2.7 on both Ubuntu and OSX, so help on either system would be appreciated.
(My apologies in advance if this is covered elsewhere, but searching for threads on "changing Python error messages" generally gave me topics on Exceptions, which is not necessarily my interest here. If anyone can point me to a page on this though, I'd greatly appreciate it.)
YES! There is a way to exactly what you want!
traceback.py is the program that detects errors in your code. It then gives you an explanation of what happened (creates the error message that you see.)
You can find this file in your library folder for python.
When in that file you can change the messages that it outputs when you come across an error!
Please tell me if this helped you!
using cProfile of python, I cprofiled my code, but I keep getting this error related with compile() and null character which I can't quite understand.
The error message is:
[cProfileV]: cProfile output available at http://127.0.0.1:4000
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/bin/cprofilev", line 9, in
load_entry_point('CProfileV==1.0.6', 'console_scripts', 'cprofilev')()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cprofilev.py", line 217, in main
code = compile(fp.read(), progname, 'exec')
TypeError: compile() expected string without null bytes
Is it a problem with my client code, or the server just isn't up?
Thank you in advance.
I believe the error is caused by a mismatch between versions of cprofile and cprofilev.
Cprofilev can be run directly using:
python -m cprofilev your_script.py
This has been asked before.
Anyhow, the error means you're recieving a string which you cannot read well, because it includes nulls. That means the server is up & responding but you cannot read the respond properly. That is because it's in another format, which is very likely JSON. try using the JSON module, included in python 2.6 and beyond; you could see some examples for it, here. If you'll provide us with your code, I can help you convert your application into a JSON-friendly one. :)
I recently installed pocketsphinx modules on my mac running Lion and Python 2.7.3(Not Apple's built in python, but brewed). I tried to run the following code:
import pocketsphinx as p
decoder = p.Decoder(jsgf='/path_to_jsgf_file/grammar.jsgf',samprate='8000')
fh = open('myrecording.wav', 'rb')
nsamp = decoder.decode_raw(fh)
print 'You said,', hyp
print 'Your score was', score
But here's the error I get on running import pocketsphinx as p :
Traceback (most recent call last):
File "speech_reco.py", line 4, in <module>
import pocketsphinx as p
File "sphinxbase.pxd", line 150, in init pocketsphinx (pocketsphinx.c:7935)
ValueError: PyCapsule_GetPointer called with invalid PyCapsule object
However, it runs correctly when the import command is run the second time. Therefore, presently, I'm running a try-catch there and getting past the error. But I don't know whether this still works.
This issue seems to have come up before,as discussed on this link: http://sourceforge.net/p/cmusphinx/bugs/284/
Has anyone fixed this issue yet? If not, would try- catch be the right way to get around this error?
Has anyone fixed this issue yet? If not, would try- catch be the right way to get around this error?
This Cython issue is fixed in later Cython
If not, would try- catch be the right way to get around this error?
The right way to get around this error is to recreate glue code with Cython. The last message in the bug report says that:
http://sourceforge.net/p/cmusphinx/bugs/284/