adafruit / Adafruit-Motor-HAT-Python-Library

100 stars 107 forks source link

This library doesn't work with Python 3 (and it's 2017) #22

Closed wroscoe closed 7 years ago

wroscoe commented 7 years ago

This should be at least noted on the repo so people waist hours trying. This port should not be too difficult and there are several branches/pull requests that have made it working for their application.

The PMW Servo Hat has a great install flow that works for python 3. Could this install guide be made to match.

I'm using this to allow differential drive cars use the donkey to make them self driving. 2017-02-12 18 58 32

wroscoe commented 7 years ago

Turns out there is an easy fix.

cd Adafruit-Motor-HAT-Python-Library
2to3  -w .
pip install . -I
samclane commented 7 years ago

@wroscoe I tried using the method you described. While the process completed, there are two noticeable problems:

  1. The library doesn't work outside of the ../Adafruit_MotorHAT_Python/ directory
  2. The example stepper code given in the tutorial fails at the line:

myStepper.step(100, Adafruit_MotorHAT.FORWARD, Adafruit_MotorHAT.SINGLE)

giving the error:

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/pi/Adafruit-Motor-HAT-Python-Library/Adafruit_MotorHAT/Adafruit_MotorHAT.py", line 158, in step lateststep = self.oneStep(direction, stepstyle) File "/home/pi/Adafruit-Motor-HAT-Python-Library/Adafruit_MotorHAT/Adafruit_MotorHAT.py", line 135, in oneStep coils = step2coils[self.currentstep/(self.MICROSTEPS/2)] TypeError: list indices must be integers, not float This is due to the way Python3 casts division to a float.

Is there a way to convert around this using 2to3 too? Or are there few enough calls that I can just manually change them?

wroscoe commented 7 years ago

@samclane

  1. I was doing this with a virtualenv activated and it does work outside that one directory.
  2. In the readout of 2to3 I remember seeing some unfixable errors in that script. I'm only using the DCMotors script so I don't have any answers for you. I learned about the 2to3 script this week.
tdicola commented 7 years ago

Ah thanks for the reminder, yeah this library needed some bigger changes because the python-smbus library was never ported to python 3. Luckily I made my own pure python version of an smbus/i2c interface that supports both py2 and 3. Just swapped over the lib to use this and fixed a few other python 3 issues (relative imports). Tested it out on both python 2 and 3 and it works great. Grab the latest master code and reinstall (I recommend first completely uninstalling the old lib with sudo pip uninstall adafruit-motorhat or sudo pip3 uninstall adafruit-motorhat for py 3).

woody1 commented 6 years ago

Hi

I am running the examples with Python 2 and it works a treat :) However, when I try python3 I am getting the following error:

pi@raspberrypi:~/Adafruit-Motor-HAT-Python-Library/examples $ sudo python3 StepperTest.py

Traceback (most recent call last): File "StepperTest.py", line 3, in from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor, Adafruit_StepperMotor ImportError: No module named 'Adafruit_MotorHAT'

Would you be able to give me some guidance on how to run this with Pythin 3.

Thanks

Woody

woody1 commented 6 years ago

Ref my last post - I installed it using sudo python setup.py install and not sudo python3 setup.py install Now works a treat - Thanks