Home » Coding » Python » Newbie packages Q
Newbie packages Q [message #15633] Sun, 07 October 2007 07:10 Go to next message
MarkyMarc  is currently offline MarkyMarc
Messages: 10
Registered: September 2007
Junior Member
Hi All,

I do not understand the packages system in python. I have read this
http://docs.python.org/tut/node8.html a 100 times and I can not get it
to work.

I make a test app like this:
*******************************
Test/
__init__.py (a empty file)
apack/
__init__.py (a empty file)
atest.py
bpack/
__init__.py (a empty file)
btest.py
*******************************
atest.py:
from Test.bpack import btest

def printA():
print "This is Atest from Apack"

def printbtest():
print btest.printB()

print printA()
print printbtest()
*******************************
btest.py:
from Test.apack import atest

def printB():
print "This is Btest from Bpack"

def printatest():
print atest.printA()

print printatest()
*******************************

All I get is an import error: ImportError: cannot import name x

I can remove the import statements an use the Test package quit normal
but when I try to make intra-pacage references I get the import error.

I must be missing something, could any one point me at what.

Thank you.
Re: Newbie packages Q [message #15660 is a reply to message #15633 ] Sun, 07 October 2007 08:16 Go to previous messageGo to next message
Steve Holden  is currently offline Steve Holden
Messages: 353
Registered: July 2007
Senior Member
MarkyMarc wrote:
> Hi All,
>
> I do not understand the packages system in python. I have read this
> http://docs.python.org/tut/node8.html a 100 times and I can not get it
> to work.
>
> I make a test app like this:
> *******************************
> Test/
> __init__.py (a empty file)
> apack/
> __init__.py (a empty file)
> atest.py
> bpack/
> __init__.py (a empty file)
> btest.py
> *******************************
> atest.py:
> from Test.bpack import btest
>
> def printA():
> print "This is Atest from Apack"
>
> def printbtest():
> print btest.printB()
>
> print printA()
> print printbtest()
> *******************************
> btest.py:
> from Test.apack import atest
>
> def printB():
> print "This is Btest from Bpack"
>
> def printatest():
> print atest.printA()
>
> print printatest()
> *******************************
>
> All I get is an import error: ImportError: cannot import name x
>
> I can remove the import statements an use the Test package quit normal
> but when I try to make intra-pacage references I get the import error.
>
> I must be missing something, could any one point me at what.
>
> Thank you.
>
Would it hep to observe that the atest and btest submodules attemot to
import each other?

There is no reason I can see for apack and bpack to be subpackages. Why
not just rename atest.py as apack.py at the same level as the Test
package's __init__.py, and in the same way make btest.py bpack.py.

Then the __init__.py can do:

from apack import printA
from bpack import printB

and your main program can do

import Test
Test.printA(...)

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline so I couldn't cat it
Re: Newbie packages Q [message #15674 is a reply to message #15660 ] Sun, 07 October 2007 09:24 Go to previous messageGo to next message
MarkyMarc  is currently offline MarkyMarc
Messages: 10
Registered: September 2007
Junior Member
On Oct 7, 2:16 pm, Steve Holden <st...@holdenweb.com> wrote:
> Would it hep to observe that the atest and btest submodules attemot to
> import each other?
>
> There is no reason I can see for apack and bpack to be subpackages. Why
> not just rename atest.py as apack.py at the same level as the Test
> package's __init__.py, and in the same way make btest.py bpack.py.
>
> Then the __init__.py can do:
>
> from apack import printA
> from bpack import printB
>
> and your main program can do
>
> import Test
> Test.printA(...)

This is away of doing it, but not a very "Package" way.
The situation is I want to make a package split up in sub dir's. And
each sub dir need to call the other.
And I cant see why it will not work, the way i do it.

And the atest and btest, shouldn't they be able to import each
other??
Re: Newbie packages Q [message #15691 is a reply to message #15674 ] Sun, 07 October 2007 10:24 Go to previous messageGo to next message
Bruno Desthuilliers  is currently offline Bruno Desthuilliers
Messages: 277
Registered: July 2007
Senior Member
MarkyMarc a écrit :
(snip)
> And the atest and btest, shouldn't they be able to import each
> other??

import is a statement. It's executed, like any other top-level code,
when the module is imported (or the script loaded into the interpreter
if it's called directly). So if A.py imports B.py and B.py imports A.py,
you do have a circular reference that can't be solved.

Anyway, circular dependencies are Bad(tm), so you *don't* want such a
situation.
Re: Newbie packages Q [message #15695 is a reply to message #15691 ] Sun, 07 October 2007 10:41 Go to previous messageGo to next message
MarkyMarc  is currently offline MarkyMarc
Messages: 10
Registered: September 2007
Junior Member
On Oct 7, 4:24 pm, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.com> wrote:
> MarkyMarc a écrit :
> (snip)
>
> > And the atest and btest, shouldn't they be able to import each
> > other??
>
> import is a statement. It's executed, like any other top-level code,
> when the module is imported (or the script loaded into the interpreter
> if it's called directly). So if A.py imports B.py and B.py imports A.py,
> you do have a circular reference that can't be solved.
>
> Anyway, circular dependencies are Bad(tm), so you *don't* want such a
> situation.

Yes it is bad and I would not do it in production. But shouldn't I be
able to call one module from another module inside a package?
Re: Newbie packages Q [message #15708 is a reply to message #15695 ] Sun, 07 October 2007 11:22 Go to previous messageGo to next message
Wildemar Wildenburger  is currently offline Wildemar Wildenburger
Messages: 73
Registered: August 2007
Member
MarkyMarc wrote:
> On Oct 7, 4:24 pm, Bruno Desthuilliers <bruno.
> 42.desthuilli...@wtf.websiteburo.oops.com> wrote:
>> MarkyMarc a écrit :
>> import is a statement. It's executed, like any other top-level code,
>> when the module is imported (or the script loaded into the interpreter
>> if it's called directly). So if A.py imports B.py and B.py imports A.py,
>> you do have a circular reference that can't be solved.
>>
>> Anyway, circular dependencies are Bad(tm), so you *don't* want such a
>> situation.
>
> Yes it is bad and I would not do it in production. But shouldn't I be
> able to call one module from another module inside a package?
>
Thats not the point. Intra-package references are (of course) perfectly
possible, the problem here are *circular* references (as Bruno explained).

Secondly, if you have such circular dependencies, I would argue that
your package design might need a little reconsideration. Why do 2
seperate modules need *each other*? To me that sounds like 2 modules
begging to be combined.

/W
Re: Newbie packages Q [message #15712 is a reply to message #15708 ] Sun, 07 October 2007 11:35 Go to previous messageGo to next message
MarkyMarc  is currently offline MarkyMarc
Messages: 10
Registered: September 2007
Junior Member
On Oct 7, 5:22 pm, Wildemar Wildenburger
<lasses_w...@klapptsowieso.net> wrote:
> MarkyMarc wrote:
> > On Oct 7, 4:24 pm, Bruno Desthuilliers <bruno.
> > 42.desthuilli...@wtf.websiteburo.oops.com> wrote:
> >> MarkyMarc a écrit :
> >> import is a statement. It's executed, like any other top-level code,
> >> when the module is imported (or the script loaded into the interpreter
> >> if it's called directly). So if A.py imports B.py and B.py imports A.py,
> >> you do have a circular reference that can't be solved.
>
> >> Anyway, circular dependencies are Bad(tm), so you *don't* want such a
> >> situation.
>
> > Yes it is bad and I would not do it in production. But shouldn't I be
> > able to call one module from another module inside a package?
>
> Thats not the point. Intra-package references are (of course) perfectly
> possible, the problem here are *circular* references (as Bruno explained).
>
> Secondly, if you have such circular dependencies, I would argue that
> your package design might need a little reconsideration. Why do 2
> seperate modules need *each other*? To me that sounds like 2 modules
> begging to be combined.
>
> /W

It was simply to make a point. But then lets say the to files looks
like this:

*******************************
atest.py:

def printA():
print "This is Atest from Apack"
*******************************
btest.py:
from Test.apack import atest

def printB():
print "This is Btest from Bpack"

def printatest():
print atest.printA()

print printB()
print printatest()
*******************************

Now only one of them imports the other, and this most be the simplest
way of illustrating the intra-package references.
But how do I get this to work?
Re: Newbie packages Q [message #15719 is a reply to message #15712 ] Sun, 07 October 2007 11:49 Go to previous messageGo to next message
Wildemar Wildenburger  is currently offline Wildemar Wildenburger
Messages: 73
Registered: August 2007
Member
MarkyMarc wrote:
> *******************************
> atest.py:
>
> def printA():
> print "This is Atest from Apack"
> *******************************
> btest.py:
> from Test.apack import atest
>
> def printB():
> print "This is Btest from Bpack"
>
> def printatest():
> print atest.printA()
>
> print printB()
> print printatest()
> *******************************
>
> Now only one of them imports the other, and this most be the simplest
> way of illustrating the intra-package references.
> But how do I get this to work?
>

This doesn't work? What error do you get?
I've never done too complicated packaging stuff, and can't raelly
testdrive your example right now. Maybe you have to look into sys.path
if Test can be found at all. Or maybe you have to play with the import
statement (from apack import atest?). I'm just guessing here; importing
continues to remain going on being a mystery to me.

/W
Re: Newbie packages Q [message #15729 is a reply to message #15719 ] Sun, 07 October 2007 11:59 Go to previous messageGo to next message
MarkyMarc  is currently offline MarkyMarc
Messages: 10
Registered: September 2007
Junior Member
On Oct 7, 5:49 pm, Wildemar Wildenburger
<lasses_w...@klapptsowieso.net> wrote:
> MarkyMarc wrote:
> > *******************************
> > atest.py:
>
> > def printA():
> > print "This is Atest from Apack"
> > *******************************
> > btest.py:
> > from Test.apack import atest
>
> > def printB():
> > print "This is Btest from Bpack"
>
> > def printatest():
> > print atest.printA()
>
> > print printB()
> > print printatest()
> > *******************************
>
> > Now only one of them imports the other, and this most be the simplest
> > way of illustrating the intra-package references.
> > But how do I get this to work?
>
> This doesn't work? What error do you get?
> I've never done too complicated packaging stuff, and can't raelly
> testdrive your example right now. Maybe you have to look into sys.path
> if Test can be found at all. Or maybe you have to play with the import
> statement (from apack import atest?). I'm just guessing here; importing
> continues to remain going on being a mystery to me.
>
> /W

I get "no module name Test.apack."
But if I print the sys.path just before importing the Test.apack, I
have this:
/python/Test/bpack
So "Test" is in my path. But it seems like it will not look up the
path but only down. And there by do not read the __init__.py files.
But I might be wrong.

Anyone that can explain me how this import and packaging in python
works??
Re: Newbie packages Q [message #15735 is a reply to message #15712 ] Sun, 07 October 2007 12:04 Go to previous messageGo to next message
Bruno Desthuilliers  is currently offline Bruno Desthuilliers
Messages: 277
Registered: July 2007
Senior Member
MarkyMarc a écrit :
(snip)
> It was simply to make a point. But then lets say the to files looks
> like this:
>
> *******************************
> atest.py:
>
> def printA():
> print "This is Atest from Apack"
> *******************************
> btest.py:
> from Test.apack import atest


FWIW, better to stick to all_lower names for packages and modules.

> def printB():
> print "This is Btest from Bpack"
>
> def printatest():
> print atest.printA()
>
> print printB()
> print printatest()
> *******************************
>
> Now only one of them imports the other, and this most be the simplest
> way of illustrating the intra-package references.
>
> But how do I get this to work?

You failed to specify how your files are organized, and what is "not
working".

But anyway, if
- atest.py is in <wherever>/Test/apack,
- both Test and apack have a __init__.py
- <wherever> is in the sys.path,

then this should just work AFAICT.
Re: Newbie packages Q [message #15744 is a reply to message #15735 ] Sun, 07 October 2007 12:18 Go to previous messageGo to next message
MarkyMarc  is currently offline MarkyMarc
Messages: 10
Registered: September 2007
Junior Member
On Oct 7, 6:04 pm, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.com> wrote:
> MarkyMarc a écrit :
> (snip)
>
> > It was simply to make a point. But then lets say the to files looks
> > like this:
>
> > *******************************
> > atest.py:
>
> > def printA():
> > print "This is Atest from Apack"
> > *******************************
> > btest.py:
> > from Test.apack import atest
>
> FWIW, better to stick to all_lower names for packages and modules.
>
>
>
> > def printB():
> > print "This is Btest from Bpack"
>
> > def printatest():
> > print atest.printA()
>
> > print printB()
> > print printatest()
> > *******************************
>
> > Now only one of them imports the other, and this most be the simplest
> > way of illustrating the intra-package references.
>
> > But how do I get this to work?
>
> You failed to specify how your files are organized, and what is "not
> working".
>
> But anyway, if
> - atest.py is in <wherever>/Test/apack,
> - both Test and apack have a __init__.py
> - <wherever> is in the sys.path,
>
> then this should just work AFAICT.

If you se me first post you will see have me files are organized. But
like this:

Test/
__init__.py (a empty file)
apack/
__init__.py (a empty file)
atest.py
bpack/
__init__.py (a empty file)
btest.py

And also in me first post(and others),
all I get is an import error: ImportError: cannot import name apack.

And sys.path is /python/Test/bpack
Re: Newbie packages Q [message #15799 is a reply to message #15744 ] Sun, 07 October 2007 13:41 Go to previous messageGo to next message
Bruno Desthuilliers  is currently offline Bruno Desthuilliers
Messages: 277
Registered: July 2007
Senior Member
MarkyMarc a écrit :
> On Oct 7, 6:04 pm, Bruno Desthuilliers <bruno.
(snip)
>> But how do I get this to work?
>> You failed to specify how your files are organized, and what is "not
>> working".
>>
>> But anyway, if
>> - atest.py is in <wherever>/Test/apack,
>> - both Test and apack have a __init__.py
>> - <wherever> is in the sys.path,
>>
>> then this should just work AFAICT.
>
> If you se me first post you will see have me files are organized.

Could have changed.

> But like this:
>
> Test/
> __init__.py (a empty file)
> apack/
> __init__.py (a empty file)
> atest.py
> bpack/
> __init__.py (a empty file)
> btest.py
>
> And also in me first post(and others),
> all I get is an import error: ImportError: cannot import name apack.
>
> And sys.path is /python/Test/bpack

And you do wonder why you can't import ? Please reread with attention
the first and third points listed above (following the 'But anyway').
The 'Test' package is *not* in your sys.path.
Re: Newbie packages Q [message #17426 is a reply to message #15799 ] Sun, 07 October 2007 15:48 Go to previous messageGo to next message
MarkyMarc  is currently offline MarkyMarc
Messages: 10
Registered: September 2007
Junior Member
On Oct 7, 8:00 pm, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.com> wrote:

>
> > And sys.path is /python/Test/bpack
>
> And you do wonder why you can't import ? Please reread with attention
> the first and third points listed above (following the 'But anyway').
> The 'Test' package is *not* in your sys.path.

I can say yes to the first:
The atest.py is in the right dir/package.
And the third. If it is not good enough that this /python/Test/bpack
is in the path.
Then I can not understand the package thing.

I also tried to put /python/ and /python/Test in the sys.path same
result.

What am I missing in the intra-package references. Can anyone provide
at working example?
Re: Newbie packages Q [message #17430 is a reply to message #17426 ] Sun, 07 October 2007 16:05 Go to previous messageGo to next message
aleax  is currently offline aleax
Messages: 95
Registered: July 2007
Member
MarkyMarc <marcsgbrevkonto@gmail.com> wrote:
...
> > > And sys.path is /python/Test/bpack

sys.path must be a LIST. Are you saying you set yours to NOT be a list,
but, e.g., a STRING?! (It's hard to tell, as you show no quotes there).

> > The 'Test' package is *not* in your sys.path.
>
> I can say yes to the first:
> The atest.py is in the right dir/package.
> And the third. If it is not good enough that this /python/Test/bpack
> is in the path.
> Then I can not understand the package thing.
>
> I also tried to put /python/ and /python/Test in the sys.path same
> result.

If the only ITEM in the list that is sys.path is the string '/python',
then any Python code you execute will be able to import Test.apack (as
well as Test.bpack, or just Test).


Alex
Re: Newbie packages Q [message #17432 is a reply to message #17430 ] Sun, 07 October 2007 16:22 Go to previous messageGo to next message
MarkyMarc  is currently offline MarkyMarc
Messages: 10
Registered: September 2007
Junior Member
On Oct 7, 10:05 pm, al...@mac.com (Alex Martelli) wrote:
> MarkyMarc <marcsgbrevko...@gmail.com> wrote:
>
> ...
>
> > > > And sys.path is /python/Test/bpack
>
> sys.path must be a LIST. Are you saying you set yours to NOT be a list,
> but, e.g., a STRING?! (It's hard to tell, as you show no quotes there).
>
> > > The 'Test' package is *not* in your sys.path.
>
> > I can say yes to the first:
> > The atest.py is in the right dir/package.
> > And the third. If it is not good enough that this /python/Test/bpack
> > is in the path.
> > Then I can not understand the package thing.
>
> > I also tried to put /python/ and /python/Test in the sys.path same
> > result.
>
> If the only ITEM in the list that is sys.path is the string '/python',
> then any Python code you execute will be able to import Test.apack (as
> well as Test.bpack, or just Test).
>

Of course I have more than just the /python string in the sys.path.
I have a list of paths, depending on which system the code run on.
Re: Newbie packages Q [message #17434 is a reply to message #17432 ] Sun, 07 October 2007 16:31 Go to previous messageGo to next message
aleax  is currently offline aleax
Messages: 95
Registered: July 2007
Member
MarkyMarc <marcsgbrevkonto@gmail.com> wrote:
...
> > > > > And sys.path is /python/Test/bpack
> >
> > sys.path must be a LIST. Are you saying you set yours to NOT be a list,
> > but, e.g., a STRING?! (It's hard to tell, as you show no quotes there).
...
> > > I also tried to put /python/ and /python/Test in the sys.path same
> > > result.
> >
> > If the only ITEM in the list that is sys.path is the string '/python',
> > then any Python code you execute will be able to import Test.apack (as
> > well as Test.bpack, or just Test).
>
> Of course I have more than just the /python string in the sys.path.
> I have a list of paths, depending on which system the code run on.

As long as '/python' comes in the list before any other directory that
might interfere (by dint of having a Test.py or Test/__init__.py), and
in particular in the non-pathological case where there are no such
possible interferences, my assertion here quoted still holds.

If you're having problems in this case, run with python -v to get
information about all that's being imported, print sys.path and
sys.modules just before the import statement that you think is failing,
and copy and paste all the output here, incuding the traceback from said
failing import.


Alex
Re: Newbie packages Q [message #17435 is a reply to message #17434 ] Sun, 07 October 2007 16:58 Go to previous messageGo to next message
MarkyMarc  is currently offline MarkyMarc
Messages: 10
Registered: September 2007
Junior Member
On Oct 7, 10:31 pm, al...@mac.com (Alex Martelli) wrote:
> MarkyMarc <marcsgbrevko...@gmail.com> wrote:
>
> ...
>
> > > > > > And sys.path is /python/Test/bpack
>
> > > sys.path must be a LIST. Are you saying you set yours to NOT be a list,
> > > but, e.g., a STRING?! (It's hard to tell, as you show no quotes there).
> ...
> > > > I also tried to put /python/ and /python/Test in the sys.path same
> > > > result.
>
> > > If the only ITEM in the list that is sys.path is the string '/python',
> > > then any Python code you execute will be able to import Test.apack (as
> > > well as Test.bpack, or just Test).
>
> > Of course I have more than just the /python string in the sys.path.
> > I have a list of paths, depending on which system the code run on.
>
> As long as '/python' comes in the list before any other directory that
> might interfere (by dint of having a Test.py or Test/__init__.py), and
> in particular in the non-pathological case where there are no such
> possible interferences, my assertion here quoted still holds.
>
> If you're having problems in this case, run with python -v to get
> information about all that's being imported, print sys.path and
> sys.modules just before the import statement that you think is failing,
> and copy and paste all the output here, incuding the traceback from said
> failing import.
>
> Alex

OK thank you, with some help from the -v option and debugging I found
a test package in some package. I now renamed it and load it with
sys.path.append.
And now the btest.py works.

BUT does this mean I have to set the path too the package in every
__init__.py class?
Or have do I tell a subpackage that it is part of a big package ?
Re: Newbie packages Q [message #17446 is a reply to message #17435 ] Sun, 07 October 2007 19:24 Go to previous message
aleax  is currently offline aleax
Messages: 95
Registered: July 2007
Member
MarkyMarc <marcsgbrevkonto@gmail.com> wrote:
...
> > As long as '/python' comes in the list before any other directory that
> > might interfere (by dint of having a Test.py or Test/__init__.py), and
> > in particular in the non-pathological case where there are no such
> > possible interferences, my assertion here quoted still holds.
> >
> > If you're having problems in this case, run with python -v to get
> > information about all that's being imported, print sys.path and
> > sys.modules just before the import statement that you think is failing,
> > and copy and paste all the output here, incuding the traceback from said
> > failing import.
> >
> > Alex
>
> OK thank you, with some help from the -v option and debugging I found
> a test package in some package. I now renamed it and load it with
> sys.path.append.
> And now the btest.py works.

Good.

> BUT does this mean I have to set the path too the package in every
> __init__.py class?
> Or have do I tell a subpackage that it is part of a big package ?

The package directory (the one containing __init__.py) must be on some
directory in sys.path, just like a plain something.py module would have
to be in order to be importable. How you arrange for this is up to you
(I normally install all add-ons in the site-packages directory of my
Python installation: that's what Python's distutils do by default, ).
As for conflict in names (of modules and/or packages), they're of course
best avoided than worked-around; not naming any module test.py, nor any
package (directory containing an __init__.py) Test, is a good start.


Alex
Previous Topic:ANN: Pyrex 0.9.6
Next Topic:Cross platform way of finding number of processors on a machine?
Goto Forum:
  


Current Time: Fri May 16 02:45:31 EDT 2008

Total time taken to generate the page: 5.15081 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 2.7.7.
Copyright ©2001-2007 FUD Forum Bulletin Board Software