How do I write good/correct package __init__.py files
in python, which of the following files is mandatory to treat a folder as a package?
when is __init__ py executed
__init__.py not working
init py search
python class __init__ best practices
My package has the following structure:
mobilescouter/ __init__.py #1 mapper/ __init__.py #2 lxml/ __init__.py #3 vehiclemapper.py vehiclefeaturemapper.py vehiclefeaturesetmapper.py ... basemapper.py vehicle/ __init__.py #4 vehicle.py vehiclefeature.py vehiclefeaturemapper.py ...
I'm not sure how the
__init__.py files should be correctly written. The
__init__.py #1 looks like:
__all__ = ['mapper', 'vehicle'] import mapper import vehicle
But how should for example
__init__.py #2 look like? Mine is:
__all__ = ['basemapper', 'lxml'] from basemaper import * import lxml
When should be
__all__ is very good - it helps guide import statements without automatically importing modules
import * is redundant, only
__all__ is needed
I think one of the most powerful reasons to use
import * in an
__init__.py to import packages is to be able to refactor a script that has grown into multiple scripts without breaking an existing application. But if you're designing a package from the start. I think it's best to leave
__init__.py files empty.
foo.py - contains classes related to foo such as fooFactory, tallFoo, shortFoo
then the app grows and now it's a whole folder
foo/ __init__.py foofactories.py tallFoos.py shortfoos.py mediumfoos.py santaslittlehelperfoo.py superawsomefoo.py anotherfoo.py
then the init script can say
__all__ = ['foofactories', 'tallFoos', 'shortfoos', 'medumfoos', 'santaslittlehelperfoo', 'superawsomefoo', 'anotherfoo'] # deprecated to keep older scripts who import this from breaking from foo.foofactories import fooFactory from foo.tallfoos import tallFoo from foo.shortfoos import shortFoo
so that a script written to do the following does not break during the change:
from foo import fooFactory, tallFoo, shortFoo
What is __init__.py for?, containing it as modules. Furthermore, this is the first file to be loaded in a module, so you can use it to execute code that you want to run each time a module is loaded, or specify the submodules to be exported. What is a Python package? A Python package is simply an organized collection of python modules. A python module is simply a single python file. Why would I want to create a package using __init__.py? Creating a package with __init__.py is all about making it easier to develop larger Python projects.
__init__.py files are empty more often than not. In particular, I never have a
from blah import * as part of
__init__.py -- if "importing the package" means getting all sort of classes, functions etc defined directly as part of the package, then I would lexically copy the contents of
blah.py into the package's
__init__.py instead and remove
blah.py (the multiplication of source files does no good here).
If you do insist on supporting the
import * idioms (eek), then using
__all__ (with as miniscule a list of names as you can bring yourself to have in it) may help for damage control. In general, namespaces and explicit imports are good things, and I strong suggest reconsidering any approach based on systematically bypassing either or both concepts!-)
6. Modules, treat directories containing the file as packages. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path. My own __init__.py files are empty more often than not. In particular, I never have a from blah import * as part of __init__.py-- if "importing the package" means getting all sort of classes, functions etc defined directly as part of the package, then I would lexically copy the contents of blah.py into the package's __init__.py instead and remove blah.py (the multiplication of source files
__init__.py should have a docstring.
Although all the functionality is implemented in modules and subpackages, your package docstring is the place to document where to start. For example, consider the python
For any other content, see the excellent answers by firecrow and Alex Martelli.
What is __init__.py? and what should I put in it?, A good module structure for the developer may or may not be a good module structure for the user. Designing for Python package import patterns best. The structure of your __init__.py file will determine that setup. In this scenario, the user gets access to everything right away on import example_pkg . Python 2 requires __init__.py files inside a folder in order for the folder to be considered a package and made importable. In Python 3.3 and above, thanks to its support of implicit namespace packages, all folders are packages regardless of the presence of a __init__.py file. In Python 2, one could write from <module> import * within
Can someone explains __all__ in Python?, The best way to understand why you would use __init__.py and to learn how to use it to create a package is to Then create an empty file in that folder called __init__.py It will sometimes also suggest the proper correction. The problem was that i made the __init__.py file by choosing the "File-> Save As" option in the Python Shell. You can solve it by either making a blank text file and save it as __init__.py or by making a new file from the python shell and then save it as __init__.py. PS: Don't forget the underscores before and after "init".
What's __init__ for me?, The cons are, the user of the package has to import seperate modules and http://stackoverflow.com/questions/1944569/how-do-i-write-good-correct-init-py- If you have package, you assume that there is a modules and code inside of it. But you will rarely count __init__.py as one, because, let's face it, most times it is just a requirement to make modules from directory importable. package __init__.py mod1.py humans.py cats.py dogs.py cars.py commons.py
How to create a Python Package with __init__.py, *__init__.py file in python* It treats directory as package and files inside it as module so Duration: 6:11 Posted: Oct 6, 2019 The problem was that i made the __init__.py file by choosing the "File-> Save As" option in the Python Shell. You can solve it by either making a blank text file and save it as __init__.py or by making a new file from the python shell and then save it as __init__.py. PS: Don't forget the underscores before and after "init".
- Be aware though that using import * in code is generally very bad practice and should be avoided if possible. There are very few good use cases for this, but they are really rare.
- PSA: if you're interested in learning how to write good namespace packages (the new kind of package), check out this example package: github.com/pypa/sample-namespace-packages
- I was very confused about 'all' and line by line import. Your example is very illuminating.
- I'm confused by "
import *is redundant",
__all__is used by the consumer of the module, and
from foo import *is used by the module itself to use others....
using __all__ and import * is redundant, only __all__ is neededHow are those redundant? They do different things.
- Personally, I prefer to keep things separate, and then import *. THe reason is that, despite folding and stuff, I still hate to browse files containing too many classes, even if related.
- @stefano think about a big framework. if it uses
import *you must unconditionally accept all the framework in its all, even features the you will never use. keeping
__init__.pyempty give you more chances than just all-or-nothing semantic. think about twisted.
- if keep it empty, even after import mobilescouter, one still can't use mobilescouter.mapper or mobilescouter.vehicle or mobilescouter.whatever. isn't import mobilescouter.A, mobilescouter.B..... too verbose?
- @sunqiang this is personal but i don't think so.
from mobilescouter import A, Bis just a line of code and you don't have a project with 666 classes and every one with his own file, right? if you have two or more
import *in your code you are filling the namespace with potential garbage and quickly you'll forget where
Acome from. And if an upper package do the same? you are grabbing all the sub-packages and sub-sub-packages. like the zen of python says, explicit is better than implicit.
- @mg, if there is a line "import A, B" in the init.py file, then I can call the A(or B) with the syntax:mobilescouter.A; if we use "from mobilescouter import A, B", then it's just A.something. sometime just this line, I don't remember A is a sub pacakge of mobilescouter, and I think this contributes to namespace pollution (though it's a lot better than ""from mobilescouter import *". I still prefer "import pkgname" give user the uniform public interface. so init.py do the import sub_pkgname things.
- Does the actual
- @Gertlex Maybe only in the web documentation.