0

so I installed pynput (pip install pynput) but it is still giving me an error that I didn't install it. Is there a reason for this? Thanks.

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from pynput import mouse
ImportError: No module named pynput
FishyMine
  • 23
  • 1
  • 9

1 Answers1

1

your pip install was pointing to a different python version than the one thats running that script

you can usually solve this by doing python -m pip install <package> (which will make sure to install it for the python version that python points to)

if you are just using system python try using python3 -m pip install XXX as pip by itself is probably referencing python2

if you are using a virtualenv in vscode you will need to activate that version with <venv_path>/bin/activate then you can run python -m pip install XXXX

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • ``` Traceback (most recent call last): File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 163, in _run_module_as_main mod_name, _Error) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 111, in _get_module_details __import__(mod_name) # Do not catch exceptions initializing package File "/Library/Python/2.7/site-packages/pip-21.1.2-py2.7.egg/pip/__init__.py", line 1, in from typing import List, Optional ImportError: No module named typing ``` – FishyMine Sep 11 '21 at 23:00
  • `python` must be pointing to python2 ... try my other suggestion`python3` (but at a guess if you are in vscode it is using a virtualenv, which you need to activate before running that command) – Joran Beasley Sep 11 '21 at 23:02
  • Its working now! Thank you so much. I had to run python3 test.py – FishyMine Sep 11 '21 at 23:04
  • @FishyMine: Another way to do a similar thing is to use `pip3 install ` instead of `python3 -m pip install `. – Sylvester Kruin Sep 11 '21 at 23:04
  • @SamMatzko in my experience especially for newer developers using `python -m pip` instead helps them avoid this problem much better – Joran Beasley Sep 11 '21 at 23:05
  • @JoranBeasley: Just wondering: how is it different, other than syntax? – Sylvester Kruin Sep 11 '21 at 23:06
  • it just helps ensure that you are usiing the python you expect :P – Joran Beasley Sep 11 '21 at 23:07
  • 2
    `python -m pip` will use the version of Python that `python` currently refers to; `pip` will use whichever version `pip` currently refers to. These can differ due to a misconfigured PATH, and possibly other reasons I'm not thinking of right now. `python3` and `pip3` will get *some* 3.x installation even if `python` and `pip` respectively would use a 2.x installation, but there are still other issues. We really need some kind of master post on managing multiple Python installations (and venvs). – Karl Knechtel Sep 11 '21 at 23:10
  • @KarlKnechtel put it much better than I did – Joran Beasley Sep 11 '21 at 23:11
  • Another issue is that on Windows, `pip` can't upgrade itself, due to (as I understand it) how Windows implements protection against self-modifying executables. `python -m pip` will work because it's now `python.exe` that's running, rather than the `pip.exe` wrapper executable. – Karl Knechtel Sep 11 '21 at 23:11
  • @KarlKnechtel: Ah, that makes sense. – Sylvester Kruin Sep 11 '21 at 23:22