简体   繁体   中英

ModuleNotFoundError when importing installed module in venv

I installed a library inside a virtual environment using venv in Visual Studio Code, then imported it into a Python file. However, when I run the file, I get a ModuleNotFoundError.

Attempted steps and checks:

  • I do not have multiple versions of Python installed.
  • I activated the venv before installing the module and before running the file.
  • VS Code says that it is using the Python executable in \venv\scripts.
  • The module is in fact in venv\Lib\site-packages .
  • I am running the latest version of pip both within and without the venv.
  • I restarted VS Code.

Oddities:

  • Running the file from the built-in terminal works rather than raising an error.
  • which python in Bash returns /c/Users/Lyndon/Documents/VS Code/seatmap_parser/\Users\Lyndon\Documents\VS Code\seatmap_parser\venv/Scripts/python when it should presumably return C:\Users\Lyndon\Documents\VS Code\seatmap_parser\venv\Scripts\python .
  • Adding the setting "python.pythonPath": "${workspaceRoot}/venv/Scripts/python" to workspace settings.json raises this error: The Python path in your debug configuration is invalid.
  • If I deactivate the venv, install the module globally, and reactivate the venv, the file now works, leading me to think that perhaps somehow the wrong Python executable is running anyway.

I stand ready to check for any missed details upon request. Why isn't it working?

If you want to get OUTPUT just clicking run in a specified file, then you have to go user settings. Enter your python path in "code-runner.executorMap " dict after "python" key. If not specified just type code-runner.executorMap and it will generate that dict for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM