[Twisted-Python] python's "import" is giving me a headache.

Chris Armstrong carmstro at twistedmatrix.com
Wed Dec 5 15:30:51 EST 2001

On Wed, 2001-12-05 at 09:52, Blake Winton wrote:
> > >import twisted.words.service
> > >from twisted import words
> > >words.Service(...)
> > A bit more thought - this is a bad idea.
> > twisted.internet.addDelayed, but also twisted.internet.StupidProxy - 
> > what's one got to do with the other? And how are they related to 
> > twisted.internet.schedule? why do I have to start reading through all 
> > these modules to figure out where they came from?)
> What about "import twisted.words.service as WordsService"?
> Then packages can name the modules whatever they want to avoid conflicts.

Well, this is a Python 2.0 (1.6?) and above feature, only. The
equivalent in older versions of Python is
| from twisted.words import service
| WordsService = service
| del service
It's still 3 lines long, just like the other few proposals that have
been going around.

Itamar definitely has a point with changing naming conventions of the
module names to something like StudlyCaps. It would probably fix at
least 95% of the cases where naming conflicts happen.

Now, say that we do something like change naming conventions -- is there
_any hope_ that old .tap files will be able to upgrade to the new names
of modules? Moshe has hinted that hooks for modulenames and whatnot is
possible with pickle, but I'm doubtful. What I'm thinking so far is that
the new module names will have to do the backwards-compatibility trick
| import twisted.my_package.MyModule
| import twisted.my_package
| twisted.my_package.module_name = twisted.my_package.MyModule

But I'm not so sure this would work out. Obviously, code that does 'from
my_package import my_module' will not work. But, as long as we're
interested in pickle-compatibility and not code-compatibility, I don't
_think_ this is a problem. Or maybe it is..?

Another possible solution is to put an __init__.py in the package that
imports all the new ModuleNames and assigns them to module_names. This
is rather yucky, of course, because you can't selectively import modules
(well, you can, but you can't prevent all modules in a package from
being imported).

Whew. a lot to think about here. I still think it's worth whatever it
takes, though, because Twisted ain't gettin' any smaller.

<radix> I was drinking tea before this job
                              Chris Armstrong
                      <<< radix at twistedmatrix.com >>>

More information about the Twisted-Python mailing list