Newbie packages Q

M

MarkyMarc

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.
 
S

Steve Holden

MarkyMarc said:
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
 
M

MarkyMarc

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??
 
B

Bruno Desthuilliers

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.
 
M

MarkyMarc

MarkyMarc a écrit :
(snip)


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?
 
W

Wildemar Wildenburger

MarkyMarc said:
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
 
M

MarkyMarc

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?
 
W

Wildemar Wildenburger

MarkyMarc said:
*******************************
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
 
M

MarkyMarc

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??
 
B

Bruno Desthuilliers

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.
 
M

MarkyMarc

MarkyMarc a écrit :
(snip)


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








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
 
B

Bruno Desthuilliers

MarkyMarc a écrit :
On Oct 7, 6:04 pm, Bruno Desthuilliers <bruno. (snip)

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.
 
M

MarkyMarc

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?
 
A

Alex Martelli

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 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
 
M

MarkyMarc

...


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).




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.
 
A

Alex Martelli

MarkyMarc said:
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
 
M

MarkyMarc

...



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 ?
 
A

Alex Martelli

MarkyMarc said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,153
Latest member
NamKaufman
Top