Can distutils create empty __init__.py files?

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?


Asked by: Leonardo840 | Posted: 24-09-2021






Answer 1

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 ?

Answered by: Dexter873 | Posted: 25-10-2021



Answer 2

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?

Answered by: Briony794 | Posted: 25-10-2021



Similar questions

import - Importing files in Python from __init__.py

Suppose I have the following structure: app/ __init__.py foo/ a.py b.py c.py __init__.py a.py, b.py and c.py share some common imports (logging, os, re, etc). Is it possible to import these three or four common modules from the __init__.py file so I don't have to import them in every one of the files? Edit: My goal is to avoid having to import 5-6 m...


python - using __init__.py

I am having difficulty understanding the usage scenarios or design goals of python's __init__.py files in my projects. Assume that I have 'model' directory (refers as a package) which contains the following files __init__.py meta.py solrmodel.py mongomodel.py samodel.py I fo...


Global function in __init__.py not accessible using Pylons + Python

I'm having trouble creating a global function accessible from within all classes. I receive an error from within user.py that says: NameError: global name 'connectCentral' is not defined Here is my current code. project/model/__ init __.py: """The application's model objects""" import sqlalchemy as sa from sqlalchemy import orm from sqlalchemy impor...


python - How to access __init__.py variables from deeper parts of a package

I apologize for yet another __init__.py question. I have the following package structure: +contrib +--__init__.py | +database +--__init__.py | +--connection.py In the top-level __init__.py I define: USER='me'. If I import contrib from the command line, then I can access contrib.USER. N...


python - __init__.py seemingly not working

New to Python. I'm on Windows and Python2.7. I have confirmed that my directory is setup in PYTHONPATH by looking in sys.path from IDLE. import sys print(sys.path) This shows me that c:\users\owner\documents\PythonProjects is definitely in the path. Furthermore in that directory I have a .py file which can be imported into IDLE. The trouble is c:\user\owner\documents\Pyth...


Can someone explain these few lines of python code file name __init__.py


__init__.py usage for directories in Python

I use __init__.py in my project with the following structure : project\ project.py cfg.py __init__.py database\ data.py __init__.py test\ test_project.py __init__.py All is OK when I need to see database\ modules in project.py with from database.data import * But if I need to have some test co...


python - How to prevent nose from importing __init__.py files?

Can the nose testing framework be instructed to only run tests in test_*.py files? In fact, doing nosetests A with the following directory structure: A/ test_A.py B/ __init__.py imports B, which I want to avoid. The reason for this is that the B module starts wi...


python - Why would I put code in __init__.py files?

I am looking for what type of code would I put in __init__.py files and what are the best practices related to this. Or, is it a bad practice in general ? Any reference to known documents that explain this is also very much appreciated.


Importing modules in Python and __init__.py

I have been reading about the function of __init__.py file. It is said that we need an empty __init__.py file in the folder which contains modules, so that these modules can be imported. However, I tried adding a folder path to PYTHONPATH (Environment Variable in Windows 7). Although this folder does not contain an __init__.py file, I can still import the modules from that folder. Cou...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top