Import di_i2c for grovepi

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?

It is in the “egg”

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).

pi@Carl:~/Carl/tmp $ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append("/home/pi/Carl/plib")
>>> print(sys.path)
['', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-arm-linux-gnueabihf', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/local/lib/python3.5/dist-packages/Adafruit-0.0.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/smbus_cffi-0.5.1-py3.5-linux-armv7l.egg', '/usr/local/lib/python3.5/dist-packages/pycparser-2.18-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/tornado-5.0.2-py3.5-linux-armv7l.egg', '/usr/local/lib/python3.5/dist-packages/pyusb-1.0.2-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/python_periphery-1.1.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/future-0.16.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/DI_Sensors-1.0.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/Line_Follower-1.0.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/wiringpi-2.46.0-py3.5-linux-armv7l.egg', '/usr/local/lib/python3.5/dist-packages/GoPiGo-0.0.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/brickpi3-0.0.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/grovepi-1.0.4-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/pivotpi-0.0.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/kalliope-0.5.4-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/voicerss_tts-1.0.6-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/urllib3-1.25.3-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/transitions-0.6.9-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/sounddevice-0.3.13-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/requests-2.22.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/PyYAML-5.1.1-py3.5-linux-armv7l.egg', '/usr/local/lib/python3.5/dist-packages/pyasn1-0.4.5-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/pyalsaaudio-0.8.4-py3.5-linux-armv7l.egg', '/usr/local/lib/python3.5/dist-packages/paho_mqtt-1.4.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/packaging-19.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/mock-3.0.5-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/MarkupSafe-1.1.1-py3.5-linux-armv7l.egg', '/usr/local/lib/python3.5/dist-packages/Jinja2-2.10.1-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/ipaddress-1.0.22-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/httpretty-0.9.6-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/gTTS-2.0.3-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/Flask_Cors-3.0.8-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/Flask-1.0.3-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/cffi-1.12.3-py3.5-linux-armv7l.egg', '/usr/local/lib/python3.5/dist-packages/APScheduler-3.6.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/ansible-2.8.1-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/SoundFile-0.10.2-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/GitPython-2.1.11-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/Flask_Testing-0.7.1-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/Flask_RESTful-0.3.7-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/logging_helper-1.8.7-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/fdutil-1.15.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/cachingutil-1.2.7-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/idna-2.8-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/chardet-3.0.4-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/certifi-2019.6.16-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/gTTS_token-1.1.3-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/bs4-0.0.1-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/tzlocal-2.0.0b2-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/gitdb2-2.0.5-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/aniso8601-7.0.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/logging_tree-1.8-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/better_exceptions-0.2.2-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/watchdog-0.9.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/stateutil-1.4.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/ruamel.yaml-0.15.97-py3.5-linux-armv7l.egg', '/usr/local/lib/python3.5/dist-packages/pyperclip-1.7.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/dominate-2.3.5-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/conversionutil-1.4.3-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/configurationutil-3.7.1-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/timingsutil-1.6.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/beautifulsoup4-4.7.1-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/smmap2-2.0.5-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/pathtools-0.1.2-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/argh-0.26.2-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/classutils-1.18.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/openpyxl-2.6.2-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/uiutil-1.36.2-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/appdirs-1.4.3-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/soupsieve-1.9.1-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/tableutil-2.4.2-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/jdcal-1.4.1-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/et_xmlfile-1.0.1-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/Dexter_AutoDetection_and_I2C_Mutex-0.0.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/gopigo3-1.2.0-py3.5.egg', '/usr/lib/python3/dist-packages',
... '/home/pi/Carl/plib']
>>> 

Here is an article I found about egg files:

2 Likes

Thanks for the info- appreciate it…

2 Likes