Troubleshooting


The following is a list of problems that you may run into when running your scripts, and suggestions for how to fix them.

Problem: You can’t find pythonon your machine.

Solution: From the command prompt, try the command which python.

If you get back a “command not found” message, try typing

 $ ls /usr/bin/python

or

 $ ls /usr/local/bin/python

If one of those commands shows that you do have python on your system, check your PATH variable and make sure it includes the directory containing python. Also check your scripts to make sure you entered the full pathname correctly If you still can’t find it, you may have to download and install python yourself, from http://www.python.org.

Problem: You get “Permission denied” when you try to run a script.

Solution: Check the permissions on your script.

For a python script to run, it needs both read and execute permissions. For instance,

 $ ./hello.py Can't open python script "./hello.py": Permission denied $ ls −1 hello.py ---x------ 1 kili           46 Apr 23 13:14 hello.py $ chmod 500 hello.py $ ls −1 hello.py -r-x------ 1 kili           46 Apr 23 13:14 hello.py $ ./hello.py Hello, World

Problem: You get a SyntaxError (“invalid syntax”).

Solution: Remember to use a colon (:) in your if statements, loops, and function definitions.

Although Python does not require curly braces around blocks of code, or semicolons at the end of each line, it does require the : before each indented block of code.

Problem: You still get an error.

Solution: Check that you are using tabs or spaces, but not both, to indent your blocks.

A common mistake is to use tabs to indent some lines and spaces to indent others. Depending on the width your editor uses to display tabs, code that appears to line up may actually be indented incorrectly If you are working on code that may be shared with other programmers, spaces are usually a safer choice than tabs. They will look the same in any editor, and a simple command such as

 $ grep "         " *.py

can be used to quickly find any cases where tabs have been used by mistake. (To include a TAB character in a command line, as in the preceding example, use CTRL-V TAB.) In addition, scripts run with python -t will generate a warning if tabs and spaces are used in the same block of code.

The command

 $ sed 's/ \t/          /g' tabfile > spacefile

replaces all the tabs in tabfile with spaces, and saves the result in spacefile. An even better solution is this simple Python program, which will replace the tabs in a list of files:

 #!/usr/bin/python # replace tabs with spaces # usage: tabreplace.py n filenames #    where n is the number of spaces to use instead of each tab import sys, fileinput # Loop through the lines in the files (starting from sys.argv[2]) # Use a special flag to send the output from print directly to the files # In each line, replace all tab characters with sys.argv[1] spaces for line in fileinput.input(sys.argv[2:], True):     print 1ine.expandtabs(int(sys.argv[1])),

The method expandtabs() actually replaces each tab with up to n spaces, so that the text will still line up correctly in columns.

Problem: You get a NameError (“name is not defined”).

Solution: Remember to import modules, and to include the module name when using its objects.

In order to use standard I/O, regular expressions, and other important language features, you must import Python modules. You must also include the module name when you use objects or functions from the module. For example, in order to use the math module to compute a square root, you would have to write

 import math sqroot = math.sqrt (121)

Problem: Test comparisons with strings fail unexpectedly.

Solution: Make sure you remove the newline at the end of strings.

Remember that strings that have been read in from files or from standard input typically have newlines at the end. If you do not remove the newlines, not only with your print statements tend to add an extra line, but your test comparisons will often fail. You can use

 str = str.rstrip ()

to remove white space (including newlines) from the end of your strings.

Problem: Running python from the command line gives an error message or no output at all.

Solution: Make sure you are enclosing your instructions in single quotes, as in

 $ python -c 'print "Hello, world"'

Problem: Running your Python script gives unexpected output.

Solution: Make sure you are running the right script!

This might sound silly, but one classic mistake is to name your script "test" and then run it at the command line only to get nothing:

 $ test $

The reason is that you are actually running /bin/test instead of your script. Try running your script with the full pathname (e.g., /home/kili/PythonScripts/test.py) to see if that fixes the problem.

Problem: Your program still doesn’t work correctly.

Solution: Try running your code with a debugger.

python comes with a command-line debugger called pdb. One way to run your script with pdb is with the -m flag, like this:

 $ python -m pdb myscript.py command-line-argument

pdb will display the first line of code in your file and wait for input. For information about the commands pdb recognizes and how to use it for debugging, see the documentation at http://docs.python.org/lib/debugger-commands.html. A list of other debuggers for Python can be found at http://wiki.python.org/moin/PythonDebuggers/.

Alternatively, you could use a Python IDE that has a graphical debugger. There are quite a few IDEs for Python, some free and some commercial, including IDLE, which ships with python. The Python wiki has a list of IDEs at http://wiki.python.org/moin/IntegratedDevelopmentEnvironments/. Many of these IDEs also have syntax checking features that can help you spot errors in your code as you work.




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net