Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #96324 > unrolled thread

Re: Question about import

Started bydieter <dieter@handshake.de>
First post2015-09-11 08:29 +0200
Last post2015-09-11 08:29 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Question about import dieter <dieter@handshake.de> - 2015-09-11 08:29 +0200

#96324 — Re: Question about import

Fromdieter <dieter@handshake.de>
Date2015-09-11 08:29 +0200
SubjectRe: Question about import
Message-ID<mailman.356.1441952977.8327.python-list@python.org>
"Frank Millman" <frank@chagford.com> writes:
>...
> My project comprises a number of modules, split into packages. Modules
> frequently need to access the contents of other modules, in the same
> or in a different package. I am getting better at it, but I still
> occasionally bump my head against circular imports, and have to fiddle
> around until it settles down again. Not ideal, I know.

Ideally, you have only oneway dependencies between modules.
In most cases, cyclic dependencies can be removed by refactoring - yielding
a better architecture.

> ...
> The surprising thing is that, within aa.py, I just have to say 'import
> b', and I can access 'b.bb.bbbb', and the same applies to 'bb.py'.

As a side effect of importing "<package>.<module>" "<module>"
is bound in "<package>". Thus, after the first import
of "<package>.<module>", "<module>" can be accessed via "<package>"
after only importing "<package>".


> That makes me wonder if, in my project, I can import all modules
> inside 'start.py', and then just use 'import package_name' inside each
> module?

You could - but you should not as this make you vulnerable with
respect to the import order.

In a small project, this is likely no problem. However, when your
projects grow and reuse components intially developped for other
projects, this may become unfeasable.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web