Using osmviz in Python - 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

Related

"AttributeError: 'module' object has no attribute" - Can someone explain the meaning of this error message?

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.

Syntax Error in python Wifi Module

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.

Possible to modify built-in error messages in Python? (If so, how?)

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!

cprofile compile error on python

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. :)

Handling errors while working with code snippets

I am running a bunch of code all at once in python by copying it from my editor and pasting it into python. This code includes nested for loops. I am doing some web scraping and the program quits at different times. I suspect that this is because it doesn't have time to load. I get the following error (once again - the program scrapes different amounts of text each time):
Traceback (most recent call last):
File "<stdin>", line 35, in <module>
IndexError: list index out of range
First, what does line 35 refer to? Is this the place in the relevant inner for-loop?
Second, I think that the error might be caused by a line of code using selenium like this:
driver.find_elements_by_class_name("button")[j-1].click()
In this case, how can handle this error? What is some example code with either explicit waits or exception handling that would address the issue?
It means that [j-1] doesn't exist for a given value of j, possibly if j-1 exceeds the max number of elements in the list
You can try your code and catch an IndexError exception like this:
try:
# your code here
except IndexError:
# handle the error here
An IndexError happens when you try to access an index of a list that does not exist. For example:
>>> a = [1, 2, 3]
>>> print(a[10])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
It's difficult to say how you should handle the error without more detail.
When working with code snippets, it's convenient to have them open in a text editor and either
only copy-paste into a console the part you're currently working on so that all the relevant variables are in the local namespace that you can explore from the console, or
copy-paste a moderate-to-large chunk as a whole while having enabled automatic post-mortem debugger calling, e.g. with Automatically start the debugger on an exception Activestate recipe or IPython's %pdb magic, or
run a script as a whole under debugger e.g with -m pdb, IPython's %run or by using an IDE.

Categories

Resources