4

I'm completely new to working with Python (I have a PHP background)

I'm used to PHP's Error Handling and Logging. Yet, with Python I'm getting a 500 Error on a very simple script that should work.

Is there a way to turn on Error Handling with Python? As 500 Error doesn't tell me much of anything, except that something is not right.

I have looked on the net for an answer to this, but I'm not finding a solution to what should be very obvious.

8
  • 2
    search for keywords try.. except in python Commented Oct 29, 2012 at 7:44
  • I'm trying to run the script from my web hosting. Any other alternatives? Commented Oct 29, 2012 at 7:52
  • @JaxonBP Have you used some frameworks? Commented Oct 29, 2012 at 7:58
  • It will help if you add more context to this question. What kind of script is it? CGI? Code that is part of a web framework? Which framework? Is any web server involved? Commented Oct 29, 2012 at 8:21
  • 1
    The first mistake you are making is thinking Python will be like PHP. PHP is a single purpose language, designed for the web. Python is a general purpose language (like C#, Java), using which you can do web development. So assuming things will work like PHP in Python is not fair and cannot be expected. In fact, PHP hides a lot of the stuff that needs to happen for web requests to be successful (stuff like showing errors). It is better to use a framework in Python and not "do it like PHP". Commented Oct 29, 2012 at 9:38

1 Answer 1

7

Your question is asking how to see errors or exceptions (not how to handle then, though of course you need to handle these errors as well), and from the 500 error and PHP background it seems to imply you are doing web programming with Python.

There are many ways to do so in Python, here is some that I tend to use:

  1. in production use logging module to log errors. There are lots of tools that can help you such as Sentry [source code here]. You can read more on logging here.
  2. in development, run your script with python -m pdb myscript.py which will start your script in debugger mode, then enter c (continue) to continue the script until error occurs, and now you will be able to see what the states are in the interactive PDB (Python debugger) prompt.
  3. in development, if you are using a framework (or your script) that relies on WSGI, you can use the graphical, interactive JavaScript based in-browser helper Werkzeug which allows you debug at every level of the call stack.

And, to point out the most obvious one, Python interpreter will print out the stack trace if the program crashes, for eg:

→ python -c 'pprint "hello"' File "<string>", line 1 pprint "hello" ^ SyntaxError: invalid syntax # print.py is just one line: print 'hello world' → python print.py File "print.py", line 1 pprint 'hello world' ^ SyntaxError: invalid syntax 

UPDATE:

It seems like you aren't using any frameworks, and you are behind a host which from the look of it, didn't tell you how exactly it is serving your Python script. Now, since all you want is to see the stack trace in the browser, you can do the following based on what your hosts uses:

If your script is running behind a server via CGI, all you need to do is to use the Python cgitb module to print the stack trace on the HTML page:

import cgitb cgitb.enable() 

However, it is very likely the shared hosting you signed up is using mod_python with Apache, so turning on PythonDebug in the Apache config file should print the stack trace in the browser:

PythonDebug On 

For Apache with mod_wsgi, there's a better article written than I could summarize here: mod_wsgi Debugging Techniques

Sign up to request clarification or add additional context in comments.

7 Comments

Exactly, If I make a mistake and write printt "Hello World!" and I load the page, I'd like to see that there is a syntax error, rather than 500 Error. This must be a built in feature of Python, right?
@JaxonBP yes, if you run this script with python -c 'pprint "hello"' you will get the stack trace which indicates where the error occurred: File "<string>", line 1 pprint "hello" ^ SyntaxError: invalid syntax (note that python myscript.py return the stack trace as well if error occurs)
If I write this in php: <? echoo 'Hello World'; I'll get this on my web browser: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/username/test.php on line 3 Is this same type of error reporting available for Python?
My problem is this - I have a script with the following: printt "Hello World!" and I don't get a stack trace as you have shown - I only get a 500 Error. Is there something that I need to enable in Python's config file to enable this? For instance, in PHP you can set error reporting on in the php.ini file. error_reporting = E_ALL display_errors = On
@JaxonBP so you are serving this script behind a server it seems, but how exactly are you running this script? You are not running it via CGI or using a framework, what are you using it with then? WSGI? FCGI? mod_python?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.