modulename.py has a class named 'classname'.
From an arbitrary python module, I 'import packagename'.
In said module, I want to use the 'classname' class from
'packagename.modulename', by doing 'packagename.classname(params)'.
I have seen this done by having either 'import modulename' and/or
'from modulename import *' in 'packagename's __init__.py. I don't
really know which one or combination of them would work, but I can't
get it to work either way. Unless I am missing something, webpy does
this with, among other things, its application class that is in
application.py. From a new web app, you would just use web.application
(params).
I am using Python 3.1.1, although I don't think that should matter.
Any help is appreciated. Thanks!
> I use distutils / setup.py to install 'packagename', where...
> /packagename
> __init__.py
> modulename.py
>
> modulename.py has a class named 'classname'.
As per PEP 8, it's best if user-defined classes are named with
TitleCase, so ‘ClassName’.
> From an arbitrary python module, I 'import packagename'.
At that point, you have all the names that were defined within
‘packagename’, available inside the namespace ‘packagename’. Since
‘modulename’ is a module in that package, the module's namespace is
available to you via ‘packagename.modulename’.
> In said module, I want to use the 'classname' class from
> 'packagename.modulename', by doing 'packagename.classname(params)'.
The name ‘ClassName’ is not in the namespace ‘packagename’. It is in the
namespace ‘packagename.modulename’, so you'll need to access it there::
import packagename.modulename
packagename.modulename.ClassName(params)
You can import the class from that namespace explicitly::
from packagename.modulename import ClassName
ClassName(params)
or you can import the module from the package namespace::
from packagename import modulename
modulename.ClassName(params)
> I have seen this done by having either 'import modulename' and/or
> 'from modulename import *' in 'packagename's __init__.py.
Importing the module in the package would mean you would not have to
import the module yourself when using the package; it would not change
the qualification necessary of the namespace.
Importing using the ‘from foo import *’ form is usually a bad idea,
since it clobbers namespaces and makes it impossible to tell by looking
at the import code where the name ‘ClassName’ appeared from. Best to
import only explicit names, as above.
If you want to abbreviate, you can use the ability to rename while
importing::
import packagename.modulename as foo
foo.ClassName(params)
which keeps the names explicit and traceable.
--
\ “Give a man a fish, and you'll feed him for a day; give him a |
`\ religion, and he'll starve to death while praying for a fish.” |
_o__) —Anonymous |
Ben Finney
I guess I'm (still) wondering how it is done in webpy. I recall seeing
it done elsewhere too.
All I noticed was that in webpy's package 'web', it defines the
'application' class in 'application.py'.
And in web's __init__.py it has...
from application import *
And in whatever app you are creating, it has...
import web
app = web.application(params)
That being said, I can't get similar functionality with my own
package. Is there more to this? Within my package's __init__.py, I am
unable to import a module from the package without an import error.
Thanks again!
However, I just set up a Debian VM with Python 2.5.2 and what I was
trying to do works. So it is either something that changed with Python
3.1.1, or a problem with Windows.
In Python 3.x absolute import is on by default. Change
from application import *
to
from .application import *
to indicate that the application module is located within the current
package.
Peter