Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do packages installed with `pip install -e .` not need __init__.py?

I recently discovered that a package which was installed with

$ pip install -e .

did not have an __init__.py file in a subpackage. Still, I could import it without problems. When I installed the same with

$ pip install .

I could not import the subpackage. Why?

(In case it matters: I use Python 3.6)

like image 736
Martin Thoma Avatar asked Dec 03 '25 10:12

Martin Thoma


1 Answers

Folders with no __init__.py may be treated as part of an implicit namespace package. Unless you know what a namespace package is and specifically want one, you should still include __init__.py in your packages, but that's what's going on here.

With pip install -e ., pip installs a thing that tells Python to look directly in your original source folder for the package contents. When Python looks in your source folder, it finds the no-__init__.py folders.

With pip install ., pip only installs what setup.py says to install, and your setup.py does not say to install the no-__init__.py folders. Python looks in the installed version of the package, not your original source folder, and it does not find the no-__init__.py folders.

like image 73
user2357112 supports Monica Avatar answered Dec 05 '25 05:12

user2357112 supports Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!