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


Groups > comp.lang.python > #80858

Re: caught in the import web again

From dieter <dieter@handshake.de>
Subject Re: caught in the import web again
Date 2014-11-16 08:14 +0100
References <m48lfh$n88$1@dont-email.me>
Newsgroups comp.lang.python
Message-ID <mailman.15871.1416122062.18130.python-list@python.org> (permalink)

Show all headers | View raw


"Charles T. Smith" <cts.private.yahoo@gmail.com> writes:
> Now, I'm getting these errors:
>
>   ImportError: cannot import name ...
>
> and
>
>   AttributeError: 'module' object has no attribute ...
>
> (what is 'module'?)
>
> Is there a way to resolve this without having to restructure my code 
> every couple of days?
>
> I thought using imports of the form:
>
>   from module import symbol
>
> was the "right way" to avoid these hassles...

I have not noticed your previous posts regarding import problems --
thus, I may repeats things already said.

Usually, Python imports are straight forward and do not lead to
surprises. There are two exceptions: recursive imports and
imports in separate threads. Let's look at the problem areas in turn.

Recursive imports. In this case you have module "A" which imports module "B"
(maybe indirectly via a sequence of intervening imports) which imports
module "A". In this, "import" means either "import ..." or
"from ... import ..." (it does not matter). When module "B" tries
to import module "A", then "A" is not yet complete (as during the
execution of "A"'s initialization code, it started to import "B"
(maybe indirectly) and the following initialization code has not yet
been executed). As a consequence, you may get "AttributeError"
(or "ImportError") when you try to access things from "A".
To avoid problems like this, try to avoid recursive imports.
One way to do this, are local imports -- i.e. imports inside
a function. This way, the import happens when the functions is called
which (hopefully) happens after module initialization.

Imports and threads. The import machinery changes global data structures
(e.g. "sys.modules"). To protect those changes, it uses a lock, held
while an import is in progress. When, during an import, a separate
thread tries to import something, it blocks - waiting for the import
lock to be released. In case, the importing thread waits on this
thread, then the system deadlocks.
To avoid this: do not start threads during imports.

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


Thread

caught in the import web again "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2014-11-15 22:52 +0000
  Re: caught in the import web again Ben Finney <ben+python@benfinney.id.au> - 2014-11-16 10:43 +1100
  Re: caught in the import web again Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-16 01:11 +0000
  Re: caught in the import web again dieter <dieter@handshake.de> - 2014-11-16 08:14 +0100
    Re: caught in the import web again "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2014-11-16 08:53 +0000
      Re: caught in the import web again Chris Angelico <rosuav@gmail.com> - 2014-11-16 20:20 +1100
      Re: caught in the import web again dieter <dieter@handshake.de> - 2014-11-17 08:08 +0100
        Re: caught in the import web again "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2014-11-17 20:07 +0000
          Re: caught in the import web again Dave Angel <davea@davea.name> - 2014-11-17 17:17 -0500
          Re: caught in the import web again Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-17 15:52 -0700
          Re: caught in the import web again Dave Angel <davea@davea.name> - 2014-11-17 20:20 -0500
          Re: caught in the import web again Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-18 08:28 -0700
          Re: caught in the import web again Dave Angel <davea@davea.name> - 2014-11-18 14:09 -0500
          Re: caught in the import web again Chris Angelico <rosuav@gmail.com> - 2014-11-19 07:18 +1100
          Re: caught in the import web again Dave Angel <davea@davea.name> - 2014-11-18 17:58 -0500
      Re: caught in the import web again Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-18 09:45 +1100
        Re: caught in the import web again Rick Johnson <rantingrickjohnson@gmail.com> - 2014-11-17 18:17 -0800
          Re: caught in the import web again Chris Angelico <rosuav@gmail.com> - 2014-11-18 13:25 +1100
          Re: caught in the import web again Steven D'Aprano <steve@pearwood.info> - 2014-11-18 07:13 +0000
        Re: caught in the import web again Michael Torrie <torriem@gmail.com> - 2014-11-18 00:00 -0700
          Re: caught in the import web again "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2014-11-18 09:13 +0000

csiph-web