Developing Techniques (and naming conventions)

A

Adekoba

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

Christian Heimes

Adekoba said:
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
 
A

Adekoba

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

Jeroen Ruigrok van der Werven

-On [20080224 20:01] said:
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.
 
A

Adekoba

-On [20080224 20:01] said:
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.

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

Steve Holden

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

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top