I know there are a ton of how-to import Python modules not in path, but I have yet to come across using Python's __init.py__ vs sys.path.insert. Which method is better? Are there any obvious drawbacks to either, like performance? Is one more "Pythonic?"
One scenario I can think of, is I have a program that users download and put in whatever directory, so I don't know the absolute path (unless I get it programatically). The folder structure is
working dir
__init__.py
foo.py
src/
my_utils.py
__init__.py
I don't see any difference between using __init__.py or changing sys.path. Is there any scenario you can think of where it would make a difference?
Part 2 of my question is, why do you have to do anything to be able to import modules from subdirectories? I'm fairly new to Python, so maybe I'm not understanding why fiddling with the path or creating init files is boilerplate. To me, it seems like an unnecessary complication. If I have "dir" in my current working directory and say "import dir.my_utils," I don't see why I would have to list everything I would want to be able to import in __init__.py.
Apologies if this is a duplicate, but I did search before posting.
Edit: Here's another useful link: Automatically call common initialization code without creating __init__.py file
Most of the time, appending to sys. path is a poor solution. It is better to take care of what your PYTHONPATH is set to : check that it contains your root directory (which contains your top-level packages) and nothing else (except site-packages which i).
The sys. path. append() method is used to temporarily add a path and as such, that path would be valid for a session for example.
sys. path is a built-in variable within the sys module. It contains a list of directories that the interpreter will search in for the required module. When a module(a module is a python file) is imported within a Python file, the interpreter first searches for the specified module among its built-in modules.
The __init__.py files are required to make Python 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.
__init__.py is used by the python interpreter to treat directories as packages. Packages play an important role in avoiding namespace collisions. If you read the section 6.4 Packages from Python Modules, it helps to prevent directories with a common name from hiding other valid modules that occur later in the search path.
Hence, the package mechanism simplifies the task of importing packages. By using an __init__.py
you can also do something like from package.subpackage import *
which would be difficult or tedious to do if we were to keep appending to sys.path
(in fact we will have to append all the possible modules).
As to answer the second part to your question - why do we need to do anything to treat directories as packages - well, there needs to be some way to tell python what should be allowed to import and what should not be. Also, you do not need to append anything to sys.path explicitly in case you have imported all the required modules at the beginning and that the modules you require to be imported are already present on the PYTHONPATH environment variable.
Hopefully this answer sheds some light on your query.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With