[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Developing Techniques (and naming conventions

Adekoba

2/24/2008 5:08:00 PM

I have some questions... Say we have a package "food"

food/
__init__.py
ham.py
cheese.py

Is it possible to have a file named "food.py" in the package and have
non-referential imports work? i.e., for ham.py to have a line "import
food.food". From my experience, python will not work with this.


Alright, new scenario. Say we had:

food.py
food/
__init__.py
ham.py
cheese.py

where food.py is a script that uses the package food. Is it possible
for this to work in any way? Every time I try to run food.py, python
tries to import everything from the script instead of from the
package. How can I fix this?
5 Answers

Christian Heimes

2/24/2008 6:06:00 PM

0

Adekoba wrote:
> food.py
> food/
> __init__.py
> ham.py
> cheese.py
>
> where food.py is a script that uses the package food. Is it possible
> for this to work in any way? Every time I try to run food.py, python
> tries to import everything from the script instead of from the
> package. How can I fix this?

Move the code from food.py to food/__init__.py. You can't have a package
and a module with the same name.

Christian

Adekoba

2/24/2008 6:59:00 PM

0

On Feb 24, 1:06 pm, Christian Heimes <li...@cheimes.de> wrote:
> Adekoba wrote:
> > food.py
> > food/
> > __init__.py
> > ham.py
> > cheese.py
>
> > where food.py is a script that uses the package food. Is it possible
> > for this to work in any way? Every time I try to run food.py, python
> > tries to import everything from the script instead of from the
> > package. How can I fix this?
>
> Move the code from food.py to food/__init__.py. You can't have a package
> and a module with the same name.
>
> Christian

So it is not possible?

I don't think moving food.py's code to __init__.py would work out to
well, because then how would I run the script?

Jeroen Ruigrok van der Werven

2/24/2008 8:21:00 PM

0

-On [20080224 20:01], Adekoba (adekoba@gmail.com) wrote:
>I don't think moving food.py's code to __init__.py would work out to
>well, because then how would I run the script?

import food

Which in turn has something like this in food/__init__.py:

from food.cheese import gouda
from food.ham import parma

__all__ = ['gouda', 'parma']

and in turn in your script you can now use food.gouda and food.parma.

Unless I totally misunderstood what you are trying to achieve.

--
Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org> / asmodai
ã?¤ã?§ã?«ã?¼ã?³ ã?©ã?¦ã??ã?­ã??ã?¯ ã?´ã?¡ã?³ ã??ã?« ã?¦ã?§ã?«ã?´ã?§ã?³
http://www.in-n... | http://www.ra...
We have met the enemy and they are ours...

Adekoba

2/24/2008 8:28:00 PM

0

On Feb 24, 3:21 pm, Jeroen Ruigrok van der Werven <asmo...@in-
nomine.org> wrote:
> -On [20080224 20:01], Adekoba (adek...@gmail.com) wrote:
>
> >I don't think moving food.py's code to __init__.py would work out to
> >well, because then how would I run the script?
>
> import food
>
> Which in turn has something like this in food/__init__.py:
>
> from food.cheese import gouda
> from food.ham import parma
>
> __all__ = ['gouda', 'parma']
>
> and in turn in your script you can now use food.gouda and food.parma.
>
> Unless I totally misunderstood what you are trying to achieve.
>
> --
> Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org> / asmodai
> ????? ?????? ??? ?? ??????http://www.in-n...|http://www.ra...
> We have met the enemy and they are ours...

I was just curious if I could get a script name to be the same name as
the package directory, but apparently it is not possible. Instead I
think I am going to do something like:

setup.py
food/
__init__.py
ham.py
cheese.py
scripts/
food

and just set my PYTHONPATH to the parent directory. I have never done
a full scale project before in python, but I think this is probably on
track with what most people would do.

Steve Holden

2/24/2008 9:44:00 PM

0

Adekoba wrote:
> On Feb 24, 1:06 pm, Christian Heimes <li...@cheimes.de> wrote:
>> Adekoba wrote:
>>> food.py
>>> food/
>>> __init__.py
>>> ham.py
>>> cheese.py
>>> where food.py is a script that uses the package food. Is it possible
>>> for this to work in any way? Every time I try to run food.py, python
>>> tries to import everything from the script instead of from the
>>> package. How can I fix this?
>> Move the code from food.py to food/__init__.py. You can't have a package
>> and a module with the same name.
>>
>> Christian
>
> So it is not possible?
>
> I don't think moving food.py's code to __init__.py would work out to
> well, because then how would I run the script?

You run food/__init__.py with the following statement:

import food

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.hold...