python-send-buffer (Emacs 24, Windows7) - python

I've been experimenting with Python a little bit under Emacs and when I have a buffer containing Python code I type C-c C-c to try and evaluate it. What I get is:
>>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'emacs' is not defined
What could I do to resolve this? I've done a little bit of digging but sadly not been able to fix my issue. In the *scratch* buffer I get the following output:
data-directory
"c:/emacs/emacs-24.0.90/etc/"
(getenv "PYTHONPATH")
nil
Which I presume is partly used to try and pick up emacs.py (which does exist in c:/emacs/emacs-24.0.90/etc/"
Any pointers greatly appreciated.

Related

Spyder Trace Debugging - Python

I am having a weird situation where the trace for an AttributeError keeps referencing the exact same absolute line number in my code.
Traceback (most recent call last):
File "<ipython-input-59-513669e63f3e>", line 4, in <module>
"line of code being run"
File "modulepath", line 148, in "method that contains the line of code"
"content of the line causing the error"
AttributeError: 'list' object has no attribute 'attribute_name'
If I make line 148 empty (by putting in new lines, etc.) or if I put a comment on line 148, the trace keeps pointing to line 148 as the source of the error.
I am having trouble debugging this particular error in general since I am (to the best of my knowledge) not using that particular attribute on any list object in my code (but it seems to indicate that I'm doing so). I will figure out that issue on my own. My main question is: what causes Spyder to repeatedly reference the exact same absolute line number in a traceback? Thank you.
When in doubt, use a fresh new console - that worked for me.

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.

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

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!

Python exception backtrace tells me where line ends, where does it begin?

When A Python exception is thrown by code that spans multiple lines, e.g.:
myfoos = [foo("bar",
"baz",
"quux",
i) for i in range(10)]
Python will report the line number of the last line, and will show the code fragment from that line:
Traceback (most recent call last):
File "test.py", line 4, in <module>
i) for i in range(10)]
NameError: name 'foo' is not defined
Is there any way to determine what the first line is? Is there any way to catch the exception and manipulate the traceback object to be able to report something like this instead:
Traceback (most recent call last):
File "test.py", lines 1-4 in <module>
myfoos = [foo("bar",
"baz",
"quux",
i) for i in range(10)]
NameError: name 'foo' is not defined
Finding the beginning of the line will be really hard. You'll have to either parse the Python or maybe dig into the compiled byte code. There are modules in the standard library for parsing Python, but I can tell you from experience that interpreting their output is a black art. And I'm not sure the compiled byte code has the answer either...
In a try/except block you can except NameError and try setting NameError.lineno, though I'm not exactly sure if or how this works, but it's the best I've found thusfar.
try:
somecode
except NameError
NameError.lineno = [1,4]
You'll have to figure out where the statement begins and ends yourself somehow as well as which statement is raising the error.
Hope this helps

Categories

Resources