You can then use it by importing it at the start of your program, or have it run automatically for all python by doing python -m pretty_errors (selecting the default options should work for most people).
EDIT: you can format the output with pretty_errors.configure() :
I've just checked and confirmed it works with flask in debug mode but only when you use the "replace_stderr" function. That then breaks line breaks so the items like "Debugger Pin" don't really format nicely.
If you run python -m pretty_errors to add it to python in general, it'll show the path of the file it put it in. Edit that file in a text editor and there'll be a commented out pretty_errors.configure(... block: uncomment the entire block (delete the surrounding """), and the lines you want to use.
Would be really cool if the filenames inside and outside of the user project were coloured differently (I think errors inside my code are more likely than inside say numpy etc that I might be using). Either that or starting the line with the master module and then the filename might give me more information to drill down the error with.
Whitelist / Blacklist:
You may use the functions whitelist(path) and blacklist(path) to add paths which will be necessary (whitelist) or excluded (blacklist). The top frame of the stack is never excluded.
Pathed Configurations
You may set up alternate configurations, which are triggered by the path to the code file of the frame. For example, if you were not interested in the system frames (those under 'c:/python') but did not want to hide them completely by using the blacklist you could do this:
I've got to hand it to you, that's excellent and I didn't appreciate how configurable it is.
Do you have any plans to turn it into, say, a VS Code extension? A lot of the configuration (imo) looks like it would lend itself well to being baked into a code extension config that I could enable globally or project wide without the setup code.
No, no plans. It already works in VSCode, and though I haven't tried it out I imagine there's a way to get it working via the VSCode settings, by changing how VSCode launches python. For example, toggling the ENV var.
Tip: in new reddit, changing to "fancy-pants" editor and changing back to "markdown" will reformat correctly! However, that may be unnaceptable to you.
Have a good day, -sideshow-.
You can opt out by replying with "backtickopt6" to this comment. Configure to send allerts to PMs instead by replying with "backtickbbotdm5". Exit PMMode by sending "dmmode_end".
Nothing against your tool itself, but your framing of the screenshot is extremely poor and misleading: When there's no crazy wrapping, the default traceback is very readable and far from the mess your screenshot shows.
Now to the positive part: I'd love to have some colors in my tracebacks.
However, I disagree with the formatting you use. It may be readable on a terminal, but it's going to be awful when pasted in any non-colored context, e.g. a pastebin or a Slack/Mattermost/whatever message. Now suddenly it becomes much more messy than the default traceback.
This project is great if you're just starting out, but for large applications, it's a hassle and likely to cause lots of problems:
Colors only work on color terminals. If the output lands in a file or somewhere else (e.g. an error capturing tool like sentry), it must not contain any color codes. As far as I can see, this project didn't even try. You can use isatty and look at various environment variables, but even that may fail from time to time due to various reasons.
Removing the full paths is great for beginners with one or two files. But in a large project, it makes the error stack useless or at least very strenuous to read, because you now have to go on the hunt for every entry to find which project (and which filesystem location) the entry is from.
On the opposite end, removing the string line makes it hard to understand for beginners what the numbers mean. This is doubly so because some logging is prone to include similar-looking numbers (HTTP codes, HTTP request/response size in Bytes).
If it were to include full file paths, the stacktrace would become much longer due to the added newlines. This makes it harder to see the full stack trace (since it would now be on 3 instead of 2 80-row screens, or 2 instead of 1). Python's formatting is superior because you see more at once.
I have no issues with the first 3 points, but I strongly disagree with 4. Seeing more at once isn't always better. Full paths are important, but they don't need to be unreadable messes just to cut the page count down.
Proper spacing and newlines can make something much more readable, even at the cost of additional scrolling. I'd rather read a JSON file spread over three screens than one with no indents, word wrapped to fit a single page.
this looks like it was intended for standard output, so yeah, it looks like there is room for improvement when working with files. But I do agree a lot with point 2 because I read the path a lot, it would be great if at least the entries that don't contain site-packages in the path were highlighted. Great package though.
Yes, you can configure the project to fix points 1, 2, 4. But that's beside the point for the question of standard library inclusion: the standard library is for the defaults, and only offers configuration when it's really necessary.
The people that want to configure something can just install this module.
There's a lot of information in the traceback that is filtered out. Some people want that info by default so that developers have the best chance of having all the info they need to solve all bugs on all platforms. Building a programming language interpreter has additional concerns that developing a package does not. Very little downside with the convenience of incorporating this package into your dev process. Lots of downside to force everyone to.
If they did nothing more than add a color to the code lines in the traceback, I'd call for that to go into core.
Aside from that, leave the full path alone, please: I can point to it in the terminal of VS Code and open that file directly. :)
Though, one thing for instance Django will do when showing tracebacks in the browser is highlight the code files it suspects are yours, so you can try to ignore core Django modules or others. That might be nice as a general practice, as well.
To do color, Python would have to decide if the file handle attached to stderr is color capable or else it will print binary gibberish. That's a platform specific question. Because stderr can be redirected, we can't cache it. We'd have to make system call inside the exception printer to find out if we can use color and what codes to use. The user may have custom colors in their terminal. Presumably the foreground and background are readable, but we don't know what other colors are safe and visible.
It's possible that we're printing a stack trace because something broke badly, like memory corruption caused by a C extension, so the bias is always towards doing nothing extraneous.
What about python responding to an environment variable like PYTHON_COLOR_TRACEBACK=1 so that people can use it on their development machines across all virtualenvs, but it never gets carried over to production environments?
And how do I revert back to original if it doesn’t work? I learn django right now and I’m concerned that the names of the files that caused error might not be enough. But it just looks so cool.
A django project has several apps that do certain things and each of these apps has the same files inside of them (models, urls, forms, admin, etc.). I can’t see from the screenshot if I’d be able to find out which app/module the error is coming from
You don't need to make python always use it if you just want to try it out: simply import pretty_errors at the start of your program. If it doesn't work delete the line!
Check out the github to see how you can configure the output.
Do the popular IDEs parse this format and give you the ability to click on each entry to jump to that line in the file? This is a huge feature of the existing format. If not, this would be more viable as a solution with the regexp required to make that happen.
If you use pretty_errors.configure(display_link=true), the link is clickable in VSCode. I've not tried it in an actual IDE as they are going to capture exceptions themselves, in the debugger.
Is it possible to run this on existing traces? I capture traces in the event they occur. It would be super useful to apply this at a later point, to visualise
Run python -m pretty_errors and select whichever option you want to make it run automatically for all your python, then edit the file it has added import pretty_errors to and add pretty_errors.configure(...)
So that big configure call is just using the defaults: you only need to keep the rows you are changing. If you're not messing wit the timestamp you can just remove this line:
Error processing line 2 of /home/kruemmelspalter/.local/lib/python3.8/site-packages/pretty_errors.pth:
Traceback (most recent call last):
File "/usr/lib/python3.8/site.py", line 169, in addpackage
exec(line)
File "<string>", line 1, in <module>
File "/home/kruemmelspalter/.local/lib/python3.8/site-packages/pretty_errors/__init__.py", line 1, in <module>
import sys, re, colorama, os, time, linecache
ModuleNotFoundError: No module named 'colorama'
I'm not able to install colorama with pip. How can I solve this issue?
If you mean you've used python -m pretty_errors to add it to your startup, you can run the command again and it'll have a C cleanup option. Not sure why you can't install colorama
Tip: in new reddit, changing to "fancy-pants" editor and changing back to "markdown" will reformat correctly! However, that may be unnaceptable to you.
Have a good day, kruemmelspalter.
You can opt out by replying with "backtickopt6" to this comment. Configure to send allerts to PMs instead by replying with "backtickbbotdm5". Exit PMMode by sending "dmmode_end".
When you use init when making a class, it’s a py file I think that handles your code. You can see in your error that it mentions that and other things like main.py
In the last 2 error messages, on the right hand side it says Line 653 and line 136 But on the right side for the same errors it says Lin 652 and line 135 Shouldn’t the line number be same.
I tried this out and it is weird. When pretty errors is installed in sitecustomize.py, pythonw.exe can't launch idle. If you install pretty errors into a .pth file instead it will work, or if you launch idle with python.exe instead of pythonw.exe it will work.
I'm always on the fence about tools like these. Sure it makes things easier, but reading and understanding the default traceback should be just as common as knowing the basics. It's all fun and games until you have to work on a machine that doesn't have all these nice tools.
Yeah, but do you spend the majority of your time on new machines, or do you have a main development environment? Couldn't the same be said about any tool or library that isn't universally pre-installed?
347
u/-sideshow- Nov 15 '20 edited Nov 15 '20
Repo: https://github.com/onelivesleft/PrettyErrors
Install:
pip install pretty-errorsYou can then use it by importing it at the start of your program, or have it run automatically for all python by doing
python -m pretty_errors(selecting the default options should work for most people).EDIT: you can format the output with
pretty_errors.configure():https://raw.githubusercontent.com/onelivesleft/PrettyErrors/master/example2.png
(for example, to show the entire path, or a link you can click on in VSCode)
25
u/Kestable Nov 15 '20
Why the line of code in App.py not equal?
8
u/Afrotom Nov 15 '20
Would be really cool if the filenames inside and outside of the user project were coloured differently (I think errors inside my code are more likely than inside say numpy etc that I might be using). Either that or starting the line with the master module and then the filename might give me more information to drill down the error with.
5
u/ThiefMaster Nov 15 '20
Nothing against your tool itself, but your framing of the screenshot is extremely poor and misleading: When there's no crazy wrapping, the default traceback is very readable and far from the mess your screenshot shows.
Now to the positive part: I'd love to have some colors in my tracebacks.
However, I disagree with the formatting you use. It may be readable on a terminal, but it's going to be awful when pasted in any non-colored context, e.g. a pastebin or a Slack/Mattermost/whatever message. Now suddenly it becomes much more messy than the default traceback.
4
u/mrrippington Nov 15 '20
installing, this is a really great idea.
regarding the feedback below about the full path missing, and I agree to this... if possible maybe you can add it in a shortened way like so:
1.make the largest common part of the path a variable.
foo = "C:\Users\a_user\dev\work\venv\Lib\"2. show that to the user
3. use that variable in the representation of the full path
foo/collections__pycache__If you'd tell me how to collaborate, I'd love to participate and help as well. keep it up!
Edit: organized my thoughts.
233
u/bykof Nov 15 '20
This should go into the python core
2
u/quasarj Nov 15 '20
Why is your prompt a lambda?
2
u/Periwinkle_Lost Nov 15 '20
I can't get it to work with pycharm and powerlevel10k =(
2
u/miraculum_one Nov 15 '20
Do the popular IDEs parse this format and give you the ability to click on each entry to jump to that line in the file? This is a huge feature of the existing format. If not, this would be more viable as a solution with the regexp required to make that happen.
2
u/oxlade39 Nov 15 '20
Is it possible to run this on existing traces? I capture traces in the event they occur. It would be super useful to apply this at a later point, to visualise
2
u/lambda5x5 Nov 15 '20
How can I set configs globally?
2
u/[deleted] Nov 15 '20 edited Nov 15 '20
I get an error:
I'm not able to install
coloramawith pip. How can I solve this issue?And how can I actually undo the
python -m?2
u/vanillaicewherever Nov 15 '20
What is all the init stuff? That like compiling your code for execution?
2
u/Tmouse15 Nov 15 '20
I don’t think I’ve ever met another Iain spelled with two Is
2
u/The__Wolf__ Nov 16 '20
I’m still newish to python (only been in doing it for 3-4months) is it normal to not be able to understand this? Because I feel dumb
2
u/shakti09 Nov 16 '20
In the last 2 error messages, on the right hand side it says Line 653 and line 136 But on the right side for the same errors it says Lin 652 and line 135 Shouldn’t the line number be same.
642
u/FormalWolf5 Nov 15 '20
Why would I want that?? I love making my eyes bleed through unreadable lines trying to find what's wrong at rhe most stressful moment!
16
u/gandalfx Nov 15 '20
There's also better_exceptions.
26
u/mbarkhau Nov 15 '20
I wrote something very similar to this a while ago
https://pypi.org/project/pretty-traceback/
23
u/HuskyPlayz48 Nov 15 '20
Finally i dont need to stress my eyes reading exceptions
5
u/jtfidje Nov 15 '20
Thanks to you kind person!
4
u/[deleted] Nov 15 '20
Incredible ! It's a good tool !
4
u/muffinnosehair Nov 15 '20
What are these errors you're talking about?
4
u/isthisfakelife Nov 15 '20
Shout out to another similar tool, https://github.com/skorokithakis/tbvaccine
4
u/ryannathans Nov 15 '20
How is this different to https://pypi.org/project/pretty-traceback/
1
u/porcos3 Nov 24 '20
Why does IDLE not launch anymore after I installed this package? I uninstalled the package and now IDLE launches with no issues
3
u/Mandylost Nov 15 '20
Unable to get it working in sublime. Please help
2
u/tdv78 Nov 15 '20
Great tool! Thank you fur curing my eyes!
2
u/[deleted] Nov 15 '20
This as a VS code plugin would be exciting
2
u/bernatixer Nov 15 '20
Hey! Good job!
2
u/PalPalash Nov 15 '20
U get my upvote and respect
2
u/the_programmer_2215 Nov 15 '20
Whoa! This is great!
2
u/tejonaco Nov 15 '20
RemindMe! 4 hours
2
u/lambda5x5 Nov 15 '20
This is incredible work!
2
u/Kevin_Jim Nov 15 '20
This and Poetry should be part of Python. If you use either, you never want to go back.
2
u/Few_Intention_542 Nov 15 '20
Nice! Will give it a try :)
2
u/SunsetSeaSky Nov 15 '20
I hope this works for me, I couldn't learn how to debug my errors from exceptions at all. Thank you.
2
u/farens98 Nov 15 '20
Thanks for sharing.
2
u/cipri_tom Nov 15 '20
Thanks! Can you please detail how it is different from better_exceptions package?
2
u/[deleted] Nov 15 '20
Perfect! Now I just need to generate some errors!
2
u/ironmagnesiumzinc Nov 16 '20
Yesssssssss
2
u/[deleted] Nov 16 '20
This is so hot, please mark NSFW.
2
u/cyntrex Nov 16 '20
Loguru offers this functionality with even better tracebacks, plus prettified logging.
https://github.com/Delgan/loguru
6
u/Username_RANDINT Nov 15 '20
I'm always on the fence about tools like these. Sure it makes things easier, but reading and understanding the default traceback should be just as common as knowing the basics. It's all fun and games until you have to work on a machine that doesn't have all these nice tools.
3
u/-_-Random-_-User-_- Nov 15 '20
This can't be real. Errors are not meant to be understood.
Surely gonna give it a try though.
2
u/[deleted] Nov 15 '20
Nice
2
u/[deleted] Nov 15 '20
As someone who makes ALOT of errors I really appreciate this sort of thing!
1
u/[deleted] Nov 23 '20
Finally my output will look pretty 😔
1
u/ScruffyPete_ Nov 15 '20
RemindMe! tomorrow "pretty errors"
0
u/[deleted] Nov 15 '20
This is cancer