python 2.7 - Import Madness: using absolute imports doesn't work -
recently, working on project, had sandbox, developing part of application. idea develop features in sandbox, , after tests, move proper project packages. please, not discuss approach, main case import problem, not work organisation.
using approach, structure of sandbox:
sandbox | +- importer | +-- __init__.py +-- ca | +-- __init__.py +-- ca.py +-- zombie.py
content of files:
importer/__init__.pyimport importer.caimporter/ca/__init__.pyfrom ca import *importer/ca/ca.pyimport importer.ca.zombie zombie
all other files empty.
after running python interpreter:
>>> import importer traceback (most recent call last): file "<stdin>", line 1, in <module> file "importer\__init__.py", line 1, in <module> import importer.ca file "importer\ca\__init__.py", line 1, in <module> ca import * file "importer\ca\ca.py", line 2, in <module> import importer.ca.zombie zombie attributeerror: 'module' object has no attribute 'ca' there sandbox directory in sys.path.
when delete content of importer/ca/ca.py, i'm able import importer. moreover, when change content of importer/ca/ca.py import importer.ca.zombie or import zombie or from importer.ca import zombie work fine. going further, using 1 of option work, print out module cache:
>>> import sys >>> in sorted(sys.modules.keys()): print i get:
... errno exceptions functools genericpath importer importer.ca importer.ca.ca importer.ca.importer importer.ca.zombie importer.importer linecache locale nt ... importer.ca.importer , importer.importer nones. , how did ther go there?
so, summarize, question not how make work, why works way. why can't use absolute import path import importer.ca.zombie zombie , what's going on in sys.module cache?
Comments
Post a Comment