ImportError: cannot import name 'InputDevice' - Evdev - python

I installed evdev on my Raspberry Pi 3 B+ with sudo -H pip install evdev.
The installation went normally and I ran python /usr/local/lib/python2.7/dist-packages/evdev/evtest.py to see if it was working. Everything was fine.
The issue is that when executing this Python code:
from evdev import InputDevice, categorize, ecodes
gamepad = InputDevice('/dev/input/js0')
print(gamepad)
for event in gamepad.read_loop():
print(categorize(event)
I get this error as an answer:
Traceback (most recent call last):
File "evdev1.py", line 1, in <module>
from evdev import InputDevice, categorize, ecodes
File "/home/pi/Desktop/evdev.py", line 2, in <module>
from evdev import InputDevice, categorize, ecodes
ImportError: cannot import name 'InputDevice'
But when I execute from evdev import InputDevice, categorize, ecodes on a python shell it seems to work.
What am I doing wrong? How can I solve this?
Thankfully, Davi.

It's written in your error traceback: You have a file called evdev1.py and a file evdev.py in your working directory. An from evdev import ... in evdev1.py would try to import from the file "/home/pi/Desktop/evdev.py" - which is also a module.
That's why calling from evdev import ... from within another working directory works.

It's a little confusing, but there are old and new methods of accessing gamepads/joysticks in linux. The older joydev shows devices as "js*" while the newer evdev shows them as "event*"
If you're using evdev, you need to use the event path starting with "/dev/input/event" instead of the one starting with "/dev/input/js".
Identify the device like this:
import evdev
devices = [evdev.InputDevice(path) for path in evdev.list_devices()]
for device in devices:
print(device.path, device.name)
Or if there is only one, just use the first one that comes along.
import evdev
gamepad = evdev.InputDevice( evdev.list_devices()[0] )

Related

Can I use CircuitPython countio library on Raspberry pi 4B?

I'm trying to read pulse with the 'countio' library from CircuitPython. When I'm importing the library, it says:
Traceback (most recent call last):
File "fanfan.py", line 2, in
import countio
ModuleNotFoundError: No module named 'countio'
Here is the codes I used:
import board
import countio
# Count rising edges only.
pin_counter = countio.Counter(board.D1, edge=countio.Edge.RISE)
# Reset the count after 100 counts.
while True:
if pin_counter.count >= 100:
pin_counter.reset()
print(pin_counter.count)
I tried
pip3 install countio
but it's not working.
Is there any way to install the countio library on Raspberry pi 4B?
Of course it fails. Because it simply does NOT exist: click me
A very simpl look up on the homepage of CircuitPython revails the actual libraries:
So, to answer your question: No, there is no way to install countio library on any platform, because it does not exist.

Trying to play an MP3 through python with playsound. Not working

For context, I have a raspberry pi model4b, and an NFC reader attached to it. When the NFC reader detects a specific card, I want it to play a specific audio file.
I have used playsound, pygame and pydub (after installign their libraries) and I keep on getting errors. Here is my play.py file:
#!/usr/bin/env python
# import required module
from playsound import playsound
# for playing note.wav file
playsound('file.mp3')
print('playing sound using playsound')
this is my full error:
pi#tjobbe:~/storytime $ sudo python play.py
Traceback (most recent call last):
File "play.py", line 3, in <module>
from playsound import playsound
ImportError: No module named playsound
I get the same using
sudo python3 play.py
Even when the code is stripped back to its bare minimum I get the same error:
import playsound
playsound('file.mp3')

Raspberry Pi Pico ImportError: no module named 'machine'

I am running the following blink program on my raspberry pi pico. I am using circuit python.
from machine import Pin
import time
led = Pin(13, Pin.OUT)
while True:
led(1)
time.sleep(1)
led(0)
time.sleep(1)
When I run it though it gives this error:
Traceback (most recent call last):
File "code.py", line 1, in <module>
ImportError: no module named 'machine'
I have tried to find if I need to download a library file or any thing about the machine module, but I have found nothing. If you know why it can't find the machine module that would be greatly appreciated.
Your code is for micropython. Circuitpython is different. See here https://learn.adafruit.com/circuitpython-essentials/circuitpython-digital-in-out
from digitalio import DigitalInOut, Direction, Pull
led = DigitalInOut(board.LED)
I was having same issue and a search found this thread.
I changed the Interpreter setting in Thonny (under options) from Local Python 3, to MicroPython (Raspberry Pi Pico)
I think you are using the incorrect uf2 file as the official (stable) version is not out as of writing this.
In order to check this, type the following in MircoPython's command line:
import sys
sys.implementation
(name='micropython', version=(1, 19, 1), _machine='Raspberry Pi Pico with RP2040', _mpy=4102)
If the response shows "Pico" and not "Pico W" then copy the latest version from here and copy it onto your Pico W (in USB mode)

How to fix "from pexpect_serial import SerialSpawn" error with Python on Windows?

I have had an old script which utilises pexpect, pyserial modules. I had this running absolutely fine before i reinstalled windows. I now cannot get it to function without displaying the following;
Traceback (most recent call last):
File "C:\Program Files\Guidance Automation Ltd\kingpiN Programming\KingpinProgramming.py", line 7, in <module>
from pexpect_serial import SerialSpawn
File "C:\Program Files\Python37\lib\site-packages\pexpect_serial\__init__.py", line 1, in <module>
from .serial_spawn import SerialSpawn
File "C:\Program Files\Python37\lib\site-packages\pexpect_serial\serial_spawn.py", line 24, in <module>
from pexpect import spawn
ImportError: cannot import name 'spawn' from 'pexpect' (C:\Program Files\Python37\lib\site-packages\pexpect\__init__.py)
I have tried multiple combinations of installing the pexpect_serial, pexpect, and pyserial modules, and multiple versions of python but still no avail.
It appears to be an issue with the pexpect-serial module.
The start of the file appears as this;
import sys
import time
import os
import serial
import pexpect.fdpexpect
import pexpect.popen_spawn
from pexpect_serial import SerialSpawn
import paramiko
from config import *
So, in answer to my specific question...
I did not need that module! I simply modified my code to;
import sys
import time
import os
import serial
import pexpect.fdpexpect
import pexpect.popen_spawn
#from pexpect_serial import SerialSpawn
import paramiko
from config import *
Hence it ran all the way through no issues!
So in hindsight, always check modules are actually needed before frustratingly trying to force them to work.
Further to this, I believe "from pexpect_serial import SerialSpawn" would not function at all on Windows.

python pyusb import usb.core doesn't work

I am following the tutorial(http://pyusb.sourceforge.net/docs/1.0/tutorial.html)
I am on windows xp sp3,
my python version is 2.7 and I downloaded and installed the pyusb-1.0.0-a1.zip
and libusb-win32-bin-1.2.4.0.zip
import usb
works fine
but
import usb.core
doesn't working at all
it says
Traceback (most recent call last):
File "D:\py\usb.py", line 1, in <module>
from usb import core
File "D:\py\usb.py", line 1, in <module>
from usb import core
ImportError: cannot import name core
any solution?
thanks!
p.s.
"from usb import core"
this make
Traceback (most recent call last):
File "D:\py\usb.py", line 1, in <module>
from usb import core
File "D:\py\usb.py", line 1, in <module>
from usb import core
ImportError: cannot import name core
full source code is here
from usb import core
#find device
dev = usb.core.find(idVendor=0x1516, idProduct=0x8628)
#found?
if dev is None :
raise ValueError('device not found')
#set the active config. with no args, the first config will be the active one
dev.set_configuration()
#get an end point instance
ep = usb.util.find_descriptor(
dev.get_interface_altsetting(), #first interface
#match the first Out Endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_OUT)
assert ep is not None
while(1):
ep.write(0x5553424350DDBC880000000000000600000000000000000000000000000000)
ep.write(0x5553425350ddbc880000000000)
Your question says you're using 1.0, but I had the same symptoms as you did, so I'll put this here for future search-engine users.
If you can import usb but not import usb.core you may be running python-usb 0.x instead of 1.0.
https://github.com/walac/pyusb
In both cases error is:
Traceback (most recent call last):
File "D:\py\usb.py", line 1, in <module>
which means it has file usb.py in PATH earlier (probably in . which is D:\py\ in this case) than path to python modules.
Did you install this module properly? Try rename this usb.py file to something else, you'll see if the error becomes "ImportError: No module named usb". Also check Python install path (something like C:\Python27\) for usb folder i.e. <python_path>\lib\site-packages\usb\core.py.
I suppose that "D:\py\usb.py" is the name of your py test program.
Unfortunately this make confusion to the py compiler due to the fact that usb is also the name of the module.
Change it in usbtest.py and everything works
for understanding where python looks to import your module, you can run following code :
import sys
print(sys.path)
this will show you list of directory names that python searches for your module to import :)

Categories