- Python and Indentation
Since Python is sensitive to indentation, it's important to keep the indentation consistent across functions and methods. One common error is to use spaces as indentation for one function and then use tabs elsewhere; this will result in a fairly cryptic compiler error. To avoid this, we recommend against using tabs for indentation, or to set you editor to use soft tabs,
- I wrote a function in
filename.py. How do I execute it from the Python interpreter?
If you start Python from the same directory that
filename.py is in (or if it's in the Python path...), then you can import the contents of
filename.py as a module:
>>> import filename
With this kind of import statement, you must reference the module name followed by the function name. Alternatively, you can import the contents of
filename.py into the local namespace:
>>> from filename import *
With this kind of import statement, you no longer need to reference the module name.
- Why are dictionary keys in such a strange order? Should I worry about the order?
Dictionaries in Python (like HashMaps in Java) are implemented using hash tables. The order of the keys depends on how exactly the hashing
algorithm maps keys to buckets, and will usually seem arbitrary. Your code should not rely on key ordering, and you
should not be surprised if even a small modification to how your code uses a dictionary results in a new key ordering.
ImportError: No module named py
import, do not include the ".py" from the filename.
For example, you should say:
NameError: name 'MY VARIABLE' is not defined
Even after importing you may see this.
To access a member of a module, you have to type
MODULE NAME.MEMBER NAME, where
MODULE NAME is the name of the
.py file, and
MEMBER NAME is the name of the variable (or function) you are trying to access.
TypeError: 'dict' object is not callable
Dictionary looks up are done using square brackets: [ and ]. NOT parenthesis: ( and ).
ValueError: too many values to unpack
Make sure the number of variables you are assigning in a
for loop matches the number of elements in each item of the list.
Similarly for working with tuples.
For example, if
pair is a tuple of two elements (e.g.
pair =('apple', 2.0)) then the following code would cause the "too many values to unpack error":
(a,b,c) = pair
Here is a problematic scenario involving a
pairList = [('apples', 2.00), ('oranges', 1.50), ('pears', 1.75)]
for fruit, price, color in pairList:
print '%s fruit costs %f and is the color %s' % (fruit, price, color)
AttributeError: 'list' object has no attribute 'length' (or something similar)
Finding length of lists is done using
len(NAME OF LIST).
Changes to a file are not taking effect.
- Make sure you are saving all your files after any changes.
If you are editing a file in a window different from the one you are using to execute python, make sure you
reload(YOUR_MODULE) to guarantee your changes are being reflected.
reload works similar to
I want to use an IDE!
Ok, you can download Eclipse with pyDev and work in an IDE!
Here is how to do it in Ubuntu 9.10:
Also check out this manual for more info on Pydev.
- Go to Ubuntu Software Center and look for Eclipse and download and Install it (may take a while).
- (only for Ubuntu) Open the terminal and type in: sudo apt-get install eclipse-pde
- Wait till it installs then open Eclipse and click on help->Install New Software...
- In the top right click Add...
- Name: PyDev Location: http://pydev.org/updates
- Click OK. Select the first thing in the list and install it.
- If the install goes well restart Eclipse.
- Click Window->Preferences
- in the left side you will see Pydev, click to expand it, then click on Interpreter - Python
- Click Auto Config in the top right. Use the default selection and click OK.
- Click apply and then ok.
- Create project and dump all your files into the created src folder.
- Open your python files in Eclipse.
- Bask in the glory of a great Python IDE!