There is a simple way how can I write a log where it shows the custom error message
# importing teh module
import logging
try:
printf("GeeksforGeeks")
except Exception as Argument:
# creating/opening a file
f = open("demofile2.txt", "a")
# writing in the file
f.write(str(Argument))
# closing the file
f.close()
the error comes here
name 'printf' is not defined
though if I run this code without try & Exception:
printf("GeeksforGeeks")
the actual error jupyter showing me as
--------------------------------------------------------------------------- NameError Traceback (most recent call
last) C:\WINDOWS\TEMP/ipykernel_21560/2330216246.py in
----> 1 printf("GeeksforGeeks")
NameError: name 'printf' is not defined
So I have two questions:
I want to write this error in my file instead of the custom one.
Is there any way to use it without try:, I have a long code and I scheduled it, however if this fails I don't know why this is actually failing. I don't want to write too many try: exception: or maintain this indent issue while writing this code.
I want when this is schedule I can get the actual reason why it failed in a log file. Please help...
Related
I am trying to build a snakemake pipeline with custom python scripts.
Some of my scripts run into errors, leading to a shutdown of the pipeline.
However, while in the shell output I can barely see the end of the python error message that leads to the shutdown, this error is not logged anywhere. It is not logged in the snakemake.log that gets automatically created (only stating which script failed without giving the error message), and adding a "log: " with a folder to the rule that fails only creates an empty log.
Is there a way to access the error message, so I can solve the underlying issue?
Edit:
my current snakemake rule looks like this:
rule do_X:
input: "{Wildcard}_a"
output: "{wildcard}_b"
log: out = "{Wildcard}_stdout.log"
err = "{Wildcardd}_stderr.err"
shell: python script x.py {input}{output}
If the script fails, I recive empty logs,
The link provided by KeyboardCat's comment seems to work well.
Demonstration adapted from your code and the suggested solution:
Save this as script_x.py:
#script_x.py
print("test output in stdout made my script")
#import sys # WITH THIS COMMENTED OUT, YOU'LL GET TRACEBACK IN `test_stderr.err`
sys.stderr.write("This was made from std.err")
with open("test_b", "w") as out_file:
out_file.write("result\n")
Then your Snakefile is:
with open("test_a", "w") as out_file:
out_file.write("#trigger file")
rule all:
input: "test_b"
rule do_X:
input: "{wildcard}_a"
output: "{wildcard}_b"
log: out = "{wildcard}_stdout.log",
err = "{wildcard}_stderr.err"
shell: 'python script_x.py {input}{output} 2> {log.err} 1> {log.out}'
Execute snakemake so it uses the Snakefile. It will cause an issue because script_x.py has an error in it and fails.
In test_stderr.err, you'll see the traceback from when the script_x.py errored because sys wasn't imported as the script was written:
Traceback (most recent call last):
File "script_x.py", line 3, in <module>
sys.stderr.write("This was made from std.err")
NameError: name 'sys' is not defined
If you delete the files test_a and remove the # from in front of the import sys line inside script_x.py (line #3), it should run without error now and result in the text This was made from std.err showing up in the file test_stderr.err.
test_stdout.log will always end up with test output in stdout made by script in it because it gets created before the line containing the error in the script.
You say you tried the solution suggested at the link provided by KeyboardCat's comment; however, what did you actually try? Always best to include the variation you tried in the comment or add an update section to your OP.
I'm trying to do something a little weird. I want to save a stack trace in Python, and then when I throw an exception on some later line, I want that exception to have the stack trace that I saved earlier, rather than the default traceback. I'm trying to give the illusion that an error was thrown from a different line than it actually was thrown from.
To make this a bit more concrete, I'd like to be able to do something like
class Pipeline:
def __init__(self):
saved_traceback = None
def m1(self):
# ... Do some work ....
saved_traceback = save_traceback()
def execute(self):
try:
# .... Do Some work
except Exception as e:
raise SpecialError(saved_traceback)
And then I'd run a script like:
foo = Pipeline()
foo.m1()
foo.execute()
And I want to implement execute in such a way that if an error is raised in its execution, the traceback instead points to the call to m1. This is part of a sort of lazy pipeline building domain specific language where no work happens until you call execute, so all error messages would come from execute. Instead, I want users to see an error that points to the method that added the faulty pipeline step.
You can use traceback.format_exc() to save the traceback as a string.
import traceback
try:
print(a)
except:
traceback_str = traceback.format_exc()
print(a) will fail with NameError because a was never defined. In the except block, the most recent traceback will be saved to traceback_str as a string.
Stackoverflow posts helped me a lot with Python, however I stuck on this one. I cannot figure out how to simply skip module if it has exceptions within it. Also, if it happens I like to alter one variable so rest of code would know to skip functionality related with that module.
I have main.py which is loading my module sms.py. Here you also see my attempt which does not work:
try:
import sms
except ImportError:
print "Ok, lets skip that module"
dont_use_sms = 1
Part of sms.py which causes exception looks following:
import gammu
sm = gammu.StateMachine()
sm.ReadConfig()
try:
sm.Init() # this one to be exact
except:
raise
when I run this I get following:
Traceback (most recent call last):
File "./main.py", line 10, in <module>
import sms
File "/path/to/sms.py", line 7, in <module>
sm.Init()
gammu.ERR_DEVICENOTEXIST: {'Text': u"Error opening device, it doesn't exist.", 'Code': 4, 'Where': 'Init'}
I have tried to alter exception by putting gammu.ERR_DEVICENOTEXIST as argument, however it didn't help.
I feel that that exception should be somehow handled by sms.py and properly forwarded to main.py, but cannot figure out how.
By the way, I know what causes gammu.ERR_DEVICENOTEXIST and that is not a problem. Question is about how to continue with rest of program if it appears.
That you for suggestions.
You can also change your main.py.
Instead of:
except ImportError:
you can say:
except:
And then it should continue.
PS: Naked except statements are not good style
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.
I want to save all following Exceptions in a file. The reason why I need this is because the IDLE for python 3.1.1 in Ubuntu raises an Exception at calltipps, but close to fast, that it isn't readble. Also I need this for testing. The best, would be if I just call a function which saves all Exception to a file. Thank you! ;)
// edit:
i had looked first for a more general way! so that you do not have to place your whole code in a function or indentation. but now that worked wery well for me. although I would be still grateful, if you find a way!
thanks!
If you have a convenient main() function (whatever it's called), then you can use the logging module:
import logging
def main():
raise Exception("Hey!")
logging.basicConfig(level=logging.DEBUG, filename='/tmp/myapp.log')
try:
main()
except:
logging.exception("Oops:")
logging.exception conveniently gets the current exception and puts the details in the log:
ERROR:root:Oops:
Traceback (most recent call last):
File "C:\foo\foo.py", line 9, in <module>
main()
File "C:\foo\foo.py", line 4, in main
raise Exception("Hey!")
Exception: Hey!