1

I am still new to the workings of git so i was hoping that someone could please clear somethings up for me. My question is how this git repository: https://github.com/adafruit/Adafruit_Python_BNO055

with file located in:

https://github.com/adafruit/Adafruit_Python_BNO055/blob/master/Adafruit_BNO055/BNO055.py

can import Adafruit_GPIO.I2C? I ask this because i have not installed Adafruit_GPIO, yet i received no errors when running simpletest.py (simpletest.py is a program that tests the BNO055 sensor and imports an instance of a class from Adafruit_BNO055). I understand that simpletest.py can import BNO055 from Adafruit_BNO055 since i installed Adafruit_Python_BNO055 repository

from Adafruit_BNO055 import BNO055

but i do not understand how Adafruit_BNO055/BNO055.py can import Adafruit_GPIO.I2C cause i did not manually install/clone it

import Adafruit_GPIO.I2C as I2C

Unless Adafruit_GPIO was installed somehow when i installed Adafruit_Python_BNO055 repository?

programmer25
  • 375
  • 1
  • 2
  • 14
  • 2
    As far as Python itself is concerned, you just import a module—Git is not involved here at all. You can use Python's `pip` to *install* a module, and that in turn *can* use Git to obtain the code for the module it installs. If you use Git to clone a repository and the repository contains a script to install a module, running the script installs the module. Each piece is independent here: they just *use* each other, in whichever direction you choose to do the using. – torek May 13 '20 at 23:00
  • The repository you mentioned includes a `setup.py` script [here](https://github.com/adafruit/Adafruit_Python_BNO055/blob/master/setup.py). This script uses `pip` to install a module. Note in particular the `install_requires` line. – torek May 13 '20 at 23:01
  • @torek, Thanks for the reply. So if im understanding correctly the setup.py runs setup function and installs the necessary dependencies which is Adafruit-GPIO. When does setup.py get executed? is it executed everytime i run simpletest.py ? – programmer25 May 13 '20 at 23:21
  • That's the crucial question, *when does `setup.py` get executed*, but it's [already answered here](https://stackoverflow.com/q/1471994/1256452) :-) – torek May 13 '20 at 23:23
  • In case you get lost in reading all the details in the other question: I have to guess, because you don't say in your question, but I suspect you used `pip install Adafruit_Python_BNO055` to install this in the first place. If you did, `pip install` inspected the `setup.py` included in the package and then *also* ran `pip install` on each of the dependencies. (Or, if you used venv/virtualenv to build, that did the same thing but used the venv wrappers, etc. There's a lot of complications and a lot of history here!) – torek May 13 '20 at 23:40
  • Thank you Torek! I actually did not install with pip i cloned the repository and than i ran ```sudo python setup.py install```. I think i understand how it works now. One more question where is the Adafruit_GPIO located after install via setup.py? – programmer25 May 14 '20 at 00:31
  • `setup.py` and your Python version define where it gets installed by default. To find out where it's being loaded from, fire up Python, import the module, and print the value of `__file__` in the module. For instance: `python -c 'import requests; print(requests.__file__)'` prints `/usr/local/lib/python3.6/site-packages/requests/__init__.py` on one of my systems. – torek May 14 '20 at 00:35
  • It works! I want to edit the i2c.py file in Adafruit_GPIO. If i clone the Adafruit_GPIO and edit it in github and install it again i suppose it will replace this one correct? – programmer25 May 14 '20 at 00:49
  • setup / pip generally looks to see which version is installed and which one is required. If you increase the version number in a new release, and require a newer version in some dependent software, pip will attempt to handle this. If you make version-incompatible changes without adjusting the version numbers accordingly, things won't go so well. – torek May 14 '20 at 00:52
  • Do you know a good way to edit the i2c.py file in Adafruit_GPIO ? – programmer25 May 14 '20 at 00:59
  • Can you try `import Adafruit_GPIO.I2C as I2C` in a python saperately? I see what your are saying is absolutely correct. My guess is, the simpletest.py didn't reach this particular line, and since python is interpreted, not compiled, it won't through the error untill it reached the line. – dumbPy May 14 '20 at 01:00
  • I know nothing about Adafruit. Editing python *files* can be done in any good editor, though. I-squared-C itself is a somewhat complicated topic: see [the Wikipedia article](https://en.wikipedia.org/wiki/I%C2%B2C) for background. – torek May 14 '20 at 01:03
  • This question has been answered. @torek if input your answer ill accept it. – programmer25 May 14 '20 at 01:04
  • @torek, when i write the following command: ```sudo python -c "import Adafruit_GPIO.I2C as I2C; print(I2C)" ``` I cant enter in the Adafruit_GPIO-1.0.4-py2.7.egg since its not a directory. i cant seem to get to I2C.pyc which does seem to be a python file now that i have notice the .pyc – programmer25 May 14 '20 at 01:06
  • The `.egg` files use archive formats. Python can read these directly, and uses them to save space. You can tell pip (or setup.py) to install in "development" mode to make it install the expanded-out files. – torek May 14 '20 at 01:16

0 Answers0