gatorback
gatorback

Reputation: 1547

ModuleNotFoundError: No module named 'hid'

BACKGROUND

Attempt to install hidapi on (Intel) iMac MacOS Ventura 13.6.1

brew install hidapi

I am porting over an old python2 script to python3. Snippet:

from __future__ import print_function
import hid, sys, time, datetime, mysql.connector, os.path, commands 
h = hid.device()

the commands module is deprecated (will not work) in Python3

FAILED TEST

user@iMac% python3

Python 3.10.8 (v3.10.8:aaaf517424, Oct 11 2022, 10:14:40) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin Type "help", "copyright", "credits" or "license" for more information.

~>>> import hid

Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'hid'

pip test

user@iMac % pip -V

pip 23.3.2 from /usr/local/lib/python3.11/site-packages/pip (python 3.11)

user@iMac% pip install hid

Requirement already satisfied: hid in /usr/local/lib/python3.11/site-packages (1.0.5) user@iMac Downloads % python3 Python 3.10.8 (v3.10.8:aaaf517424, Oct 11 2022, 10:14:40) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin Type "help", "copyright", "credits" or "license" for more information.

>>> import hid

Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'hid'

QUESTIONS

  1. How to confirm 'hid' is not deprecated in python3? 'commands' is deprecated in python3

  2. If not deprecated: what tests can be performed to determine root cause of ModuleNotFoundError?

JOHN GORDON TESTS

Terminal:

pip show hid

Name: hid
Version: 1.0.5
Summary: ctypes bindings for hidapi
Home-page: https://github.com/apmorton/pyhidapi
Author: Austin Morton
Author-email: [email protected]
License: MIT
Location: /usr/local/lib/python3.11/site-packages
Requires: 
Required-by:

Python:

import sys; print(sys.path)

['', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python310.zip', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages']
>>> 

brew's list of python installs?

user@iMac % brew list | grep python

python-setuptools
[email protected]
[email protected]
mysql-connector-python

user@iMac ~ % echo "$PATH"

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/user/Android/platform-tools:/Applications/VMware Fusion.app/Contents/Public:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin

Upvotes: 0

Views: 1932

Answers (1)

pat.io
pat.io

Reputation: 23

Initial Answer

According to PyPI (Python Package Index) the hid Python module is not preinstalled and must be installed with pip: pip install hid. Pip is usually installed by default on recent versions of Python. You can check by running pip -V or, alternatively, python3 -m pip -V. If it isn't, see the installation instructions. Once the module is installed, you should have no trouble importing it.

Updated Answer

Your problem is a python version conflict. Pip is installing packages for python version 3.11 but your code is running with python version 3.10. I'm not sure how to resolve the conflict ATM. I'll update the answer again when I have time. However, there is a quick solution you can use to save yourself some trouble, although I'd still recommend resolving the versions issue.

The following steps will create and activate a virtual environment to manage python dependencies and avoid installing all your dependencies system-wide.

  1. Install VirtualEnv: pip install virtualenv
  2. Create a virtual environment in your project directory: virtualenv <NAME>
  3. Activate the virtual environment: source <NAME>/bin/activate
  4. When you're done: deactivate

All together:

pip install virtualenv
virtualenv <NAME>
source <NAME>/bin/activate

Let me know how that goes.

Upvotes: 1

Related Questions