0

Running VS Code in Windows and Python 3.9x

I created a new project folder and created a new virtual environment for this project. The first script I created is a .ipynb file (jupyter notebook), and it works fine. When I exported the script to .py file, it generates an error on first line of code:

from pynput import keyboard

ModuleNotFoundError: No module named 'pynput'

The pynput library is installed in this virtual env, which is why the .ipynb file runs fine. When I look at the lower right corner of VS Code, I can see the virtual env is activated:

enter image description here

And the terminal prompt is prepended with (proj_env). And when I run pip list, I can see the pynput package is installed. It is NOT installed in the system environment, which is why I'm guessing the script is trying to use system env, but I could be wrong.

What am I missing?

From the testing described in the comments, perhaps it has to do with a difference between how a Windows CMD window (running a vritual env) checks certain paths for packages the script wants to import vs how VS Code checks certain paths?

sinoroc
  • 18,409
  • 2
  • 39
  • 70
jub
  • 377
  • 1
  • 2
  • 14
  • Did you try this outside of vs code? – YJR Sep 14 '22 at 22:08
  • How are you invoking your .py script? – Manish Dash Sep 14 '22 at 22:08
  • @ManishDash I'm invoking .py script using play button in upper right corner of VS Code (same as CTRL+ALT+N). – jub Sep 14 '22 at 22:11
  • Try the answer on this ques : https://stackoverflow.com/questions/66612450/vs-code-run-python-in-terminal-play-button-not-working – Manish Dash Sep 14 '22 at 22:19
  • @ManishDash the play button works fine. If I comment out `from pynput import keyboard` and put in a print statement, it prints fine. Somehow python is not looking in the virtual env for the pynput library, thus not finding it, thus generating the error. – jub Sep 14 '22 at 23:25
  • @YJR I just tried in a CMD window (after activating the venv) and the script works. So why would it work here, but not in VS Code? I have run other .py scripts in VS Code. – jub Sep 14 '22 at 23:29
  • Can you try to install another package and try to import it.if it works may be this is a version incompatability issue.Or retry to create another vertual env. Might be work. Not sure. – YJR Sep 14 '22 at 23:35
  • @YJR good idea. I installed numpy and get same behavior. Will import numpy fine running a .ipynb script in VS Code, or running .py script outside VS Code (with virtual env activated), but not when running .py script in VS Code (again, w/ virtual env activated). I will create a fresh virtual env and see. – jub Sep 15 '22 at 00:02
  • Even with a fresh env, same result. From .py file in VS Code, can import some installed packages (e.g., clipboard, re, pyautogui), but not others (pynput, numpy). When running same file from CMD window, all of these packages import fine. Strange! – jub Sep 15 '22 at 00:23
  • You could check the path of `python` and `pip` with `where`, make sure it uses the correct ones. Also check the `%PATH%` environment variable. – Green绿色 Sep 15 '22 at 01:17
  • After some more playing around, I've found that with a .py file open in VS Code, the play button will do 1 of 3 functions (these are in dropdown list attached to play button): Run code, Run python file, Debug python file. Whichever function was run last is the function that pressing play button will run. That said, when I select Run python file, it executes fine and stays in the terminal window. When I select Run code, VSCode switches to output window and that's where I get the `ModuleNotFoundError: No module named 'pynput'`. – jub Sep 17 '22 at 16:17
  • Apparently 'run code' uses the CodeRunner extension, which I have installed. Seems my problem is similar (or identical) to this: https://stackoverflow.com/questions/65753702/run-code-vs-run-python-file-in-terminal-for-vscode CodeRunner is using the system installation of python, not the virtual environment, thus it cannot find packages that are only installed in the virtual env. Not sure of how exactly to specify path in settings.json so that CodeRunner is looking in the correct place. – jub Sep 17 '22 at 17:07

1 Answers1

0

Figured out the issue, which I didn't quite understand when I asked the question, and a (the?) resolution. If anything in this answer is incorrect, please correct (and educate) me, thanks.

In VSCode, there is a difference between "Run Code" (this uses CodeRunner extension) and "Run Python File" (this runs file in terminal). The play button will use whichever of these was last run (open dropdown box next to play button).

I was having the same problem as this user: Run Code vs Run Python File in Terminal for VSCODE

The project's settings.json file specifies the "python.defaultInterpreterPath" which points to the virtual environment's python.exe. So when I run the .py file in terminal, the packages that are installed only in the virtual env (and not in the global/system environment) are found.

But Run Code uses Code-Runner, which is defaulting to the global environment's python.exe and thus does not find the packages that are only in the virtual env. The solution is to specify which environment Code-Runner should use, by adding this to settings.json file: "code-runner.executorMap": { "python": "\"$pythonPath\" $fullFileName", } Since my settings.json file is already specifying the virtual environment's python interpreter, this code snippet will tell Code-runner to use the same python interpreter to execute the code.

jub
  • 377
  • 1
  • 2
  • 14