views:

446

answers:

2

If all my __init__.py files are empty, do I have to store them into version control, or is there a way to make distutils create empty __init__.py files during installation?

+5  A: 

In Python, __init__.py files actually have a meaning! They mean that the folder they are in is a Python module. As such, they have a real role in your code and should most probably be stored in Version Control.

You could well imagine a folder in your source tree that is NOT a Python module, for example a folder containing only resources (e.g. images) and no code. That folder would not need to have a __init__.py file in it. Now how do you make the difference between folders where distutils should create those files and folders where it should not ?

That's the question, can you tell distutils that a given folder should be interpreted as a package, even though it doesn't have a __init__.py file. I could always create the __init__.py files when building the distribution, but I was wondering if you could do that with distutils.
jelovirt
Yes, there is a technique for telling distutils (and any other Python code) that a folder is a package: put an __init__.py file in the folder.
Carl Meyer
+3  A: 

Is there a reason you want to avoid putting empty __init__.py files in version control? If you do this you won't be able to import your packages from the source directory wihout first running distutils.

If you really want to, I suppose you can create __init__.py in setup.py. It has to be before running distutils.setup, so setup itself is able to find your packages:

from distutils import setup
import os

for path in [my_package_directories]:
    filename = os.path.join(pagh, '__init__.py')
    if not os.path.exists(filename):
        init = open(filename, 'w')
        init.close()

setup(
...
)

but... what would you gain from this, compared to having the empty __init__.py files there in the first place?

dF
Exactly the answer I was looking for: it can be done with distutils, but you shouldn't.
jelovirt