In the grovepi.py file there is a line: import di_i2c
I am wondering how does python know where this file is? In the sys.path file, there is only
…python3.7/disit-packages/Dexter_AutoDetection_and_I2C_Mutex-0/-/–py3.7.egg
and standard /usr/local/lib/python3.7/dist-packages…
but di_i2c.py is located at
/home/pi/Dexter/lib/Dexter/RFR_Tools/miscellaneous/di_i2c.py
/home/pi/Dexter/lib/Dexter/RFR_Tools/miscellaneous/build/lib/di_i2c.py
How does python find this file, and why is the file stored in more than one location?
unzip -t /usr/local/lib/python3.5/dist-packages/Dexter_AutoDetection_and_I2C_Mutex-0.0.0-py3.5.egg
Archive: /usr/local/lib/python3.5/dist-packages/Dexter_AutoDetection_and_I2C_Mutex-0.0.0-py3.5.egg
testing: I2C_mutex.py OK
testing: auto_detect_robot.py OK
testing: auto_detect_rpi.py OK
testing: di_i2c.py OK
testing: di_mutex.py OK
testing: EGG-INFO/PKG-INFO OK
testing: EGG-INFO/SOURCES.txt OK
testing: EGG-INFO/dependency_links.txt OK
testing: EGG-INFO/requires.txt OK
testing: EGG-INFO/top_level.txt OK
testing: EGG-INFO/zip-safe OK
testing: __pycache__/I2C_mutex.cpython-35.pyc OK
testing: __pycache__/auto_detect_robot.cpython-35.pyc OK
testing: __pycache__/auto_detect_rpi.cpython-35.pyc OK
testing: __pycache__/di_i2c.cpython-35.pyc OK
testing: __pycache__/di_mutex.cpython-35.pyc OK
No errors detected in compressed data of /usr/local/lib/python3.5/dist-packages/Dexter_AutoDetection_and_I2C_Mutex-0.0.0-py3.5.egg.
How exactly it got there, I don’t know, but my guess is that the first di_i2c.py is used when developing/testing Raspbian For Robots, and when a module is deemed ready for a build it appears int the “build/lib” folder, and the process of creating and installing Raspbian For Robots makes the “egg” and publishes it into the dist-package area for the installed python(s).
Disclaimer: I am a user, not affiliated with DI or ModRobotics, so don’t take what I think as gospel.
I believe Python looks first in the local directory, then progressively down the sys.path, (I don’t know if it searches inside every egg everytime, or there it creates a hash table of all the first location of each file inside all the egg files in the std.path at the python startup - ( but then when I do sys.path.append(’/home/pi/Carl/plib’) python would have to rebuild the table, so perhaps that is one of the reasons “import carls_plib_module_x” takes a bit of time).