Myles9
Posts: 35
Joined: Fri Sep 29, 2017 12:13 am

SPI In Python 3

Thu Mar 08, 2018 12:28 am

Hello! I am new to Raspberry Pi GPIO. I got a Clever Card Kit, and all was well until I got this error.

Code: Select all

Traceback (most recent call last):
  File "01_read.py", line 4, in <module>
    import SimpleMFRC522
  File "/home/pi/clever_card_kit/SimpleMFRC522.py", line 1, in <module>
    import MFRC522
  File "/home/pi/clever_card_kit/MFRC522.py", line 6, in <module>
    import spi
ImportError: No module named 'spi'
I turned on SPI in raspi-config, and the guide says nothing else. Help!

Myles9
Posts: 35
Joined: Fri Sep 29, 2017 12:13 am

Re: SPI In Python 3

Thu Mar 08, 2018 12:55 am

Update: I installed SPI-Py. Still nothing. https://github.com/lthiery/SPI-Py

Myles9
Posts: 35
Joined: Fri Sep 29, 2017 12:13 am

Re: SPI In Python 3

Thu Mar 08, 2018 1:06 am

OK, never mind. I just ran it in Python 2. :D

MarkDH102
Posts: 411
Joined: Fri Feb 13, 2015 3:18 pm

Re: SPI In Python 3

Thu Mar 08, 2018 7:29 am

I'm afraid you'll have to get used to this Python2 or 3 thing. It's a nightmare.
People say to use Python3, but a lot of libraries don't work in it still.

I had some Bluetooth code that ran fine under Python2. Then I added a new device that had drivers that would only work under Python3.
No problem - install Python3 Bluetooth. Did it work? No. I have to do some rewriting once I've worked out how to fix the problem.

Most of my stuff is written to run under both. But like I say, it's a nightmare. I'm glad C (not C++) isn't like this!

User avatar
DougieLawson
Posts: 39872
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: SPI In Python 3

Thu Mar 08, 2018 9:51 am

MarkDH102 wrote:
Thu Mar 08, 2018 7:29 am
I'm afraid you'll have to get used to this Python2 or 3 thing. It's a nightmare.
People say to use Python3, but a lot of libraries don't work in it still.

...

Most of my stuff is written to run under both. But like I say, it's a nightmare. I'm glad C (not C++) isn't like this!
The general rule is new code should be python3. All of the RPF supplied examples are python3. You should only revert to python2 when some library you've found won't run with python3 without lots of hacking.

It WILL all get fixed by 12th April 2020 :roll: :roll: https://pythonclock.org

I'm with you on C.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

User avatar
bensimmo
Posts: 4866
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: SPI In Python 3

Thu Mar 08, 2018 12:18 pm

Try gpiozero (by RPF people)
https://gpiozero.readthedocs.io/en/stable/api_spi.html
(New to gpiozero, so feedback how it goes to them).

And/or
Pigpio http://abyz.me.uk/rpi/pigpio/python.html

I've never used it for spi, but know of them.

User avatar
DougieLawson
Posts: 39872
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: SPI In Python 3

Thu Mar 08, 2018 2:17 pm

bensimmo wrote:
Thu Mar 08, 2018 12:18 pm
Try gpiozero (by RPF people)
https://gpiozero.readthedocs.io/en/stable/api_spi.html
(New to gpiozero, so feedback how it goes to them).

And/or
Pigpio http://abyz.me.uk/rpi/pigpio/python.html

I've never used it for spi, but know of them.
There's no support in GPIOzero for RC522 RFID readers. I'm also not sure whether Joan would have added that to pigpio.

I got RC522s working with https://github.com/mxgxw/MFRC522-python and SPI-PY. Although I've now moved to using a Wemos D1 Mini to do that stuff (publishes MQTT messages to my Raspberry and subscribes to get the results).
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

User avatar
bensimmo
Posts: 4866
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: SPI In Python 3

Thu Mar 08, 2018 3:43 pm

Ah, I don't know SPI, but I assume SPI-Spy could be replace by gpiozero's backend. gpiozero.SPIDevice(**spi_args) ? maybe something to add to github as a would like.


Athough was SPI-Spy installed for Python3, or just Python2 ?
... quick skip ahead...

https://www.raspberrypi-spy.co.uk/2018/ ... pberry-pi/ any help

User avatar
DougieLawson
Posts: 39872
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: SPI In Python 3

Thu Mar 08, 2018 5:52 pm

That stuff at https://github.com/mxgxw/MFRC522-python is all python2
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

User avatar
bensimmo
Posts: 4866
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: SPI In Python 3

Thu Mar 08, 2018 9:24 pm

DougieLawson wrote:
Thu Mar 08, 2018 5:52 pm
That stuff at https://github.com/mxgxw/MFRC522-python is all python2
He's (Matt) saying it's just the print statements that need updating on the website.
Python 2 or Python 3
By default the MFRC522 library only works with Python 2. However it is easy to modify to work with Python 3. Simply edit the “MFRC522.py” file in a text editor and convert all nine of the print statements to Python 3 friendly syntax. For example,
...
Though I was on about SPI as that is the module it couldn't find (in the very first post) and hence in the second post may not have been installed for python3.
Which it can be.

Quite happy to be wrong though :-)

Return to “General discussion”