Question

If all of 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?

Was it helpful?

Solution

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?

OTHER TIPS

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 ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top