request for help
rantingrickjohnson at gmail.com
Mon Feb 18 22:08:59 CET 2013
leonardo <tampucciolina <at> libero.it> writes:
> here is the error message:
Okay, now we are on the road to solving this problem.
But first we need to take a slight detour and learn about python packaging,
because no matter what the current error is, naming a module "circle" and then
throwing it naked out into the "Python module wilderness" is complete folly; i
can assure you!
What is a Python package?
A python package is simply another layer of namespace that protects our symbols
from clashing; in this case: module identifiers.
Even noobs understand that function bodies and class bodies (bka: object
definitions) protect code from outside influences, and that modules protect the
symbols contained in one module from the symbols contained in /other/ modules,
however, we still must protect module identifiers somehow. How do we do this?
Packages to the rescue!
Your "circle.py" module needs to be under the "protective care" of a specialized
package named "geom2d" , which itself should be under the care of a specialized
package named "math", which itself should be under the "global blanket" of a
personal library package named "insertUniqueNameHere"! This is how we protect module symbols whilst simultaneously employing a logical structure in our code.
Impatient Ivan exclaimed: "So how the heck do i create the package Rick?"
Steps to create the entire package hierarchy:
Note: All package and modules names should be lowercase!
1a. Create a folder named "mylib" on the Python search path.
1b. Convert the "mylib" folder into a package by adding a file named:
"__init__.py" This will be your personal package for containing your personal
2a. Inside the "mylib" folder create another folder called "math"
2b. Convert the "mylib\math" folder to a package by adding a file named
3a. Inside the "mylib\math" folder create another folder named "geom2d"
3b. Convert the "mylib\math\geom2d" folder to a package by adding a file named
4. Inside the "mylib\math\geom2d" folder create a new file named "circlelib.py".
This is where you will place the code for computing circle data. Later you will
probably write something more useful, but for now this module is the best you have.
Now, you'll need to import the circlelib for use and this is how you do it:
## START CODE ##
from mylib.math.geom2d.circlelib import area, circumference
## END CODE ##
>From now on, if you create any more modules that deal with maths (or a subset of
math: geom) you have a place to store them intelligently. There is quite a bit
more to Python packages but what i describe above is the most fundamental aspect.
Back to your exception
Did correcting the indentation fix the problem? If not, what is the next error
More information about the Python-list