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


Groups > comp.lang.python > #96324

Re: Question about import

From dieter <dieter@handshake.de>
Subject Re: Question about import
Date 2015-09-11 08:29 +0200
References <msrah1$sju$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.356.1441952977.8327.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web