get stacktrace of a python program for debugging - python

In order to get the stacktrace of a python program, I am trying to follow this example. In the article, the author invokes the gdb as follows. However, the python version of my environment is python 3.4.4. When I type
python3.4-dbg testmyplotlib2.py &
The error message is python3.4-dbg: command not found. What's the right way to get stacktrace by using gdb.

What OS are you on? It looks like you need to install python3.4-dbg. If you are on Linux, you will need to enter:
sudo apt-get install python3.4-dbg

GDB is excellent program for debugging, but if printing traceback is the only reason you are installing GDB, do not do it, that is waay overkill. You can just import traceback and
use traceback.format_stack() to get an array of calls that lead to the location in program
use traceback.print_stack() to print it to command line
use print traceback.format_exc() to print what lead to the current exceptions (works in except clasuse)

Related

How to fix error message "from: can't read /var/mail/pwn" [duplicate]

I am running a (bio)python script which results in the following error:
from: can't read /var/mail/Bio
seeing as my script doesn't have anything to with mail, I don't understand why my script is looking in /var/mail.
What seems to be the problem here? i doubt it will help as the script doesn't seem to be the problem, but here's my script anyway:
from Bio import SeqIO
from Bio.SeqUtils import ProtParam
handle = open("examplefasta.fasta")
for record in SeqIO.parse(handle, "fasta"):
seq = str(record.seq)
X = ProtParam.ProteinAnalysis(seq)
print X.count_amino_acids()
print X.get_amino_acids_percent()
print X.molecular_weight()
print X.aromaticity()
print X.instability_index()
print X.flexibility()
print X.isoelectric_point()
print X.secondary_structure_fraction()
what is the problem here? bad python setup? I really don't think it's the script.
No, it's not the script, it's the fact that your script is not executed by Python at all. If your script is stored in a file named script.py, you have to execute it as python script.py, otherwise the default shell will execute it and it will bail out at the from keyword. (Incidentally, from is the name of a command line utility which prints names of those who have sent mail to the given username, so that's why it tries to access the mailboxes).
Another possibility is to add the following line to the top of the script:
#!/usr/bin/env python
This will instruct your shell to execute the script via python instead of trying to interpret it on its own.
I ran into a similar error when trying to run a command.
After reading the answer by Tamás,
I realized I was not trying this command in Python but in the shell (this can happen to those new to Linux).
Solution was to first enter in the Python shell with the command python
and when you get these >>>
then run any Python commands.
Same here. I had this error when running an import command from terminal without activating python3 shell through manage.py in a django project (yes, I am a newbie yet). As one must expect, activating shell allowed the command to be interpreted correctly.
./manage.py shell
and only then
>>> from django.contrib.sites.models import Site
Put this at the top of your .py file (for Python 2.x)
#!/usr/bin/env python
or for Python 3.x
#!/usr/bin/env python3
This should look up the Python environment. Without it, it will execute the code as if it were not Python code, but shell code. If you need to specify a manual location of the Python environment, put
#!/#path/#to/#python
for Mac OS just go to applications and just run these Scripts Install Certificates.command and Update Shell Profile.command, now it will work.
For Flask users, before writing the commands, first make sure you enter the Flask shell using:
flask shell

set up Path for Python (Window)

I have downloaded Atom.io for window and done setup successfully, then I download the package for running codes. but the Python result always come with an error like the picture.
Can you help me on this issue? Thank you
Try typing in:
python myprogram.py
However, you will still receive an invalid syntax error because:
print(*hi*)
should be:
print('hi')
Also, if you want to enter the python terminal, type:
python
Then you can type these commands (i.e. print("hi")) directly into the terminal instead of creating a .py file and running it.
One final point, I use the following instructions to add the python to PATH: how to add python to path in windows.
You can check this Stack Overflow thread for more details.
However, if you just want to play around with Python in Windows:
Download Python and install it.
Go to Start > type "IDLE" and click "IDLE (Python)".
Start coding! For example, type print("Hello, world!") and press the Enter key.
My first contact with Python was IDLE and I liked it a lot. Have fun!
It looks like you're running this from powershell. You can tell it's powershell because it says "Windows Powershell" on the first line of the output. Here's what I would do if I were you:
To start the python shell type python and press enter.
1a. Enter what you want to run in python, print('test') for example
If you want to get out of the python shell, enter ctrl-z and press enter to return to powershell.
To run python scripts use the command python {script}.py
Installing python on path
If you tried the above steps and got an error to the effect of "I don't recognize python" you should ensure that python is correctly installed in your PATH.
You can follow these instructions to install python correctly.

Print python version in all error messages

I have a python script that is called by the user by for example python script.py or python3 script.py. If this script raises an error, it is of importance which python version was used.
Surely, I could ask the user for the command that was used to execute script.py, however this might not resolve the python version. python could be an alias for python3.4 or python2.7.
I know that I could just print sys.version in my script. But I do not want to print it in every run of that script, just in the case where an error has occurred.
So another idea was to simply wrap the whole script in an try, except block.
import sys
try:
# original content of script.py
except:
print("Using python version {0}".format(sys.version))
raise
However that does not look very pythonic to me. So is there a more elegant solution to add sys.version to each error message?
This question is not a duplicate of How do I check what version of Python is running my script?, as I do not want to check for the version information. My script should run with all versions of python. However, a custom message (in this case sys.version) should be appended to the stacktrace.
yes.
>>> import sys
>>> sys.version
I do not want to print it in every run of that script, just in the case where an error has occurred.
Use try, except statements
You can change sys.excepthook to include the desired behavior.
Just add the following to the beginning of your script:
import sys
def my_except_hook(exctype, value, traceback):
print("Using python version {0}".format(sys.version))
sys.__excepthook__(exctype, value, traceback)
sys.excepthook = my_except_hook
If an error is raised in your code, the python version is printed in addition to the normal error message.

Python run file from command line

Yesterday I installed Python (with Numpy & Scipy) on CentOS, everthing works fine when I'm using the CLI and do some math. But now I try to execute a file, and then a weird error appears, searched all over the place but can't find the solution to fix this.
I'm running two Python versions, the version I'm using is 3.4, and I installed it at: /usr/local/bin
Then I made a file called test.py in the same directory, with this code:
import numpy
When I try to run it with:
./python3.4 -m test.py
I get this error:
/usr/local/bin/python3.4: Error while finding spec for 'test.py' (<class 'AttributeError'>: 'module' object has no attribute '__path__')
I hope somebody can lead me into the right direction, thanks in advance!
Running things like
python -m <something>
invokes Python, which then tries to import a Python Module.
From your short example it doesn't appear to be a valid Python module
Try running with python <your python code file> instead.

NetBeans and Python

When I run some python code in NetBeans, which raises an error, the output in NetBeans just gives an error message and no further information, such as line number. Is there any way to fix that?
If you can, I would run your script outside of NetBeans either with the built-in editor (IDLE) or just run it from the command line. That should give you a traceback with the error and lineno
NetBeans has issues with debugging, as other posts suggest.
added solution is debugging if don't have any compiling error

Categories