Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80864
| From | "Charles T. Smith" <cts.private.yahoo@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: caught in the import web again |
| Date | 2014-11-16 08:53 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <m49omd$924$1@dont-email.me> (permalink) |
| References | <m48lfh$n88$1@dont-email.me> <mailman.15871.1416122062.18130.python-list@python.org> |
On Sun, 16 Nov 2014 08:14:05 +0100, dieter wrote: > "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. Yes, we're talking about recursive imports here. It's a complex, object- oriented system with big classes and little classes that are strongly interrelated. I can get the imports configured properly so everything works but if I make a little change to the code, then suddenly all my imports are broken and I must painstakingly go through the whole structure, reorganizing them. Are others equally frustrated by this or is there a trick or principle that I'm missing. At this point, I guess the way I'll have to proceed is to put every class in its own file, no matter how small. Hopefully that takes care of the problem. I compared perl's object-oriented philosophy with python's and didn't like how perl *requires* you to put everything into its own file. Now it looks like python does, too, implicitly. cts www.creative-telcom-solutions.de
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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