Importing nested modules in Python
I'm trying to impo开发者_运维百科rt a few libraries into my program (which is a google AppEngine application).
Basically, I'm supposed to put all libraries in the root folder, but I've just created another folder called lib and placed them within that folder. (I've created the __init__.py
)
Imports regularly work fine by using the import lib.module
or from lib import module
, but what happens is that when I try to import a complete package, for instance a folder named pack1 with various modules in it, by calling from lib.pack1 import *
, I get this error in one of the modules who has accessed another module statically, i.e. from pack1.mod2 import sth
.
Edit: Using Python 2.7.
Edit: Error: when usingimport lib.pack1
, I get ImportError: No module named pack1.mod1
.I think that instead of from pack1.mod2
you actually want to say from lib.pack1.mod2
.
Edit: and, specifying what version of Python this is would help, since importation semantics have improved gradually over the years!
Edit: Aha! Thank you for your comment; I now understand. You are trying to rename libraries without going inside of them and fixing the fact that their name is now different. The problem is that what you are doing is, unfortunately, impossible. If all libraries used relative imports inside, then you might have some chance of doing it; but, alas, relative imports are both (a) recent and (b) not widely used.
So, if you want to use library p
, then you are going to have to put it in your root directory, not inside of lib/p
because that creates a library with a different name: lib.p
, which is going to badly surprise the library and break it.
But I have two more thoughts.
First, if you are trying to do this to organize your files, and not because you need the import names to be different, then (a) create lib
like you are doing but (b) do not put an __init__.py
inside! Instead, add the lib
directory to your PYTHONPATH
or, inside of your program, to sys.path
. (Does the GAE let you do something like this? Does it have a PYTHONPATH
?)
Second, I am lying when I say this is not possible. Strictly speaking, you could probably do this by adding an entry to sys.metapath
that intercepts all module lookups and tries grabbing them from inside of lib
if they exist there. But — yuck.
精彩评论