Why is the Adafruit Raspberry Pi Code (https://github.com/adafruit/Adafruit-Ra ... ython-Code) a collection of python scripts, none of which include an __init__.py which would register it as a package? This would be simple to implement; along with a setup.py, so that it can be installed easily, making the modules, overall, more user friendly.
Currently I'm having to work inside of the subfolder, which include a symbolic link to the Adafruit_I2C folder.
Am I just being stupid or is it worth making the python code into a package?
Shouldn't the Raspberry Pi Code be made into a package?
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- jackweath
- Posts: 2
- Joined: Tue Jul 14, 2015 11:45 am
- adafruit_support_mike
- Posts: 67485
- Joined: Thu Feb 11, 2010 2:51 pm
Re: Shouldn't the Raspberry Pi Code be made into a package?
There's a version you can install with pip, but we've found that Python's import syntax handles local and system modules in different ways. A module in the same directory as a script can be imported with:
but if it's installed as a system module you need something like:
Since we have to choose a 'correct' installation method, we prefer to go through Github. That handles small changes more gracefully than the various Linux package managers.
Code: Select all
import Adafruit_GPIO as GPIO
Code: Select all
import Adafruit_GPIO.Adafruit_GPIO as GPIO
- jackweath
- Posts: 2
- Joined: Tue Jul 14, 2015 11:45 am
Re: Shouldn't the Raspberry Pi Code be made into a package?
Working in the same directory makes sense for single modules, but it becomes hugely impractical if you try to use many modules from the Adafruit library. The "Adafruit_I2C.Adafruit_I2C" problem still does not currently exist! Try working in the same directory as any of the I2C modules, that use classes.
You have to use:
This is not the same as:
will be used, but that allows the user to use various modules from that library rather than stick to one, inside of the same folder.
The above can however be shorten with a modification inside of the __init__.py file, that imports the class automatically, since this is what the user will do anyway when using any of the modules in the library. The __init__.py file would simply have to contain:
Small changes can be easily handled with PIP, but packages do not have to be installed that way. It would allow the users to work in a directory next to the "Adafruit-Raspberry-Pi-Python-Code" package, and import whatever they need.
The x.y part is required so that the package can still be loaded in Python 3, as it doesn't allow parent imports as easily. These are not huge changes, but would allow more flexibility and make the packages easier to use overall.
You have to use:
Code: Select all
from x import y
This is not the same as:
Code: Select all
# This will only import a module, and does not import classes.
import x.y
Code: Select all
from Adafruit-Raspberry-Pi-Python-Code.Adafruit_I2C import Adafruit_I2C
The above can however be shorten with a modification inside of the __init__.py file, that imports the class automatically, since this is what the user will do anyway when using any of the modules in the library. The __init__.py file would simply have to contain:
Code: Select all
from x.y import class
The x.y part is required so that the package can still be loaded in Python 3, as it doesn't allow parent imports as easily. These are not huge changes, but would allow more flexibility and make the packages easier to use overall.
- sveinsen
- Posts: 20
- Joined: Sat Apr 12, 2014 7:31 am
Re: Shouldn't the Raspberry Pi Code be made into a package?
Create a fork that suits your needs. If you think your fork may suit others needs as well, make a pull request. If they decide not to merge your pull request, don't worry, you will still be able to merge changes from Adafruit's branch into your fork.
Please be positive and constructive with your questions and comments.