[Tutor] import and unittest
Thomas Coopman
thomas.coopman at gmail.com
Wed Jan 17 13:39:25 CET 2007
On 1/17/07, Kent Johnson <kent37 at tds.net> wrote:
>
> thomas coopman wrote:
> > On Tue, 16 Jan 2007 10:06:37 -0000
> > "Alan Gauld" <alan.gauld at btinternet.com> wrote:
> >
> >> "Thomas Coopman" <thomas.coopman at gmail.com> wrote
> >> .
> >>> I wondered if it was possible to do something like this:
> >>>
> >>> src/
> >>> -a_module/
> >>> -sub_module/
> >>> test/
> >>> -a_module/
> >>> -sub_module/
> >>>
> >> I don;t see any reason why not although its slightly more work.
> >> Personally I tend to keep the tests with the code, but thats
> >> mainly because tools such as editors tend to remember the last
> >> folder opened and its a pain navigating between the two folders.
> >>
> >> The other system I have used(in C++ not Python) is to have
> >> a test folder inside each src folder like:
> >>
> >> src/
> >> mod1/
> >> f1.py
> >> test/
> >> testf1.py
> >> mod2/
> >> f2.py
> >> f3.py
> >> test/
> >> testf1.py
> >> testf2.py
> >>
> >> etc.
> >>
> >> This minimises navigation and keeps the tests separate.
> >> Its also relatively easy to filter out the tests when it comes
> >> time to package upp the code for distribution (assuming
> >> you want to lose them!)
> >>
> >>> I have something like this but I don't know how to organize the
> >>> imports in
> >>> the tests and I don't know if this is a good idea. What do you
> >>> think?
> >> I think in Python you should create a package structure for
> >> your code so that import can find the modules more easily.
> >> But I've never tried this in Python, my Python projects are rarely
> >> big enough to warrant it.
> >>
> >>
> >
> > well, I still have problems with import
> >
> > suppose I have something like this:
> >
> > M/
> > __init__.py
> > A/
> > __init__.py
> > One.py
> > B/
> > __init__.py
> > Two.py
> >
> > One.py
> > #!/usr/bin/python
> >
> > from M.B import Two
> >
> > Two.py
> > #!/usr/bin/python
> >
> > from M.A import One
> >
> >
> > when I try to run One.py or Two.py I get import errors and I don't know
> > how to fix them. I tried to set the __all__ variable in the __init__
> > files but nothing worked so far.
>
> What error do you get?
>
> It's better to avoid this kind of circular import if you can. Rather
> than having One depend on Two and Two depend on One, look for some piece
> you can factor out into a new module that both One and Two use (or it
> may only be needed by One or Two).
>
> There are several problems with circular imports, but the technical
> problem you are having is probably like this:
>
> module One starts to execute
> One imports Two
> Two starts to execute
> Two imports One
> -- Note that at this point, the body of One has not executed, so
> anything defined in One after the import of Two is not yet defined.
> Two tries to use something in One. Since One is not fully defined, it
> gets an AttributeError.
>
> Anyway, try to split up your modules or post the exact error you get.
>
> Kent
>
>
Well I don't really
need the circular imports but I would like to know how to do the
imports correct.
Suppose that in the example that I showed only One needs Two.
So then we have this:
M/
__init__.py
A/
__init__.py
One.py
B/
__init__.py
Two.py
One.py
#!/usr/bin/python
from M.B import Two
when I run this I get this error:
ImportError: No Module named M.B
--
Thomas Coopman
Thomas.coopman at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070117/ad0f88b4/attachment.html
More information about the Tutor
mailing list