imports in __init__.py

P

Phil

I use distutils / setup.py to install 'packagename', where...
/packagename
__init__.py
modulename.py

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!
 
P

Phil

I understand all of the above, including the reasons as to why this is
bad. For purposes of experimenting, I would still like to do it.

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!
 
S

sKeeZe

I wrote my last message late last night. When I said "I am unable to
import a module from the package without an import error.", I did mean
the 'modulename' module.

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.
 
P

Phil

I wrote my last message late last night. When I said "I am unable to
import a module from the package without an import error.", I did mean
the 'modulename' module.

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.
 
P

Peter Otten

Phil said:
I wrote my last message late last night. When I said "I am unable to
import a module from the package without an import error.", I did mean
the 'modulename' module.

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
 
L

lotrpy

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’.
it looks that one need put the "import modulename" line in __init__.py
or "import packagename.modulename" in the user clent script
explicitly. or else, just 'import packagename' is not enough to use
packagename.modulename.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top