Home » Coding » Python » Newbie packages Q
| Newbie packages Q [message #15633] |
Sun, 07 October 2007 07:10  |
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 #15708 is a reply to message #15695 ] |
Sun, 07 October 2007 11:22   |
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   |
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 #15744 is a reply to message #15735 ] |
Sun, 07 October 2007 12:18   |
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 #17432 is a reply to message #17430 ] |
Sun, 07 October 2007 16:22   |
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   |
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   |
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  |
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
|
|
|
Goto Forum:
Current Time: Fri May 16 02:44:43 EDT 2008
Total time taken to generate the page: 0.67979 seconds |