简体   繁体   English

Visual Studio Code - ModuleNotFoundError,终端和 VSCode 中的不同 sys.path?

[英]Visual Studio Code - ModuleNotFoundError, different sys.path in terminal and VSCode?

I try running code in Visual Studio code but I keep getting a ModuleNotFoundError.我尝试在 Visual Studio 代码中运行代码,但我不断收到 ModuleNotFoundError。 When I run the code in my terminal or in debug mode in VS with the activated conda environment it works fine.当我在终端中运行代码或在激活 conda 环境的 VS 中以调试模式运行代码时,它工作正常。

System: Mac M1 12.3系统:Mac M1 12.3

Conda Environment, selected in Visual Studio Code. Conda 环境,在 Visual Studio Code 中选择。

I have added this我添加了这个

import os
print('cwd is %s' %(os.getcwd()))
import sys
print('executable is %s' %(sys.executable))
print('path is %s' %(sys.path))

and running in terminal gives:并在终端中运行给出:

cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/envs/conda_envNAME/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python38.zip', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/lib-dynload', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/site-packages']

running in VS via Run Python File (upper right corner button) gives:通过运行 Python 文件(右上角按钮)在 VS 中运行给出:

cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/lib/python39.zip', '/Users/USERNAME/miniforge3/lib/python3.9', '/Users/USERNAME/miniforge3/lib/python3.9/lib-dynload', '/Users/USERNAME/miniforge3/lib/python3.9/site-packages']

running in VS via Debug Python File (upper right corner button) gives:通过调试Python 文件(右上角按钮)在 VS 中运行给出:

cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/envs/conda_envNAME/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python38.zip', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/lib-dynload', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/site-packages']

I am confused - How can I get this running in VS Code?我很困惑 - 我怎样才能让它在 VS Code 中运行?

---- Update March 23 2022: ---- 2022 年 3 月 23 日更新:

I have three options我有三个选择在此处输入图像描述

When I add当我添加

    "code-runner.executorMap": {

        "python": "$pythonPath -u $fullFileName"
        
    }

to

settings.json设置.json

(see [https://www.wiseowl.co.uk/blog/s2930/module-not-found-error.htm]2 from @Kyouya Sato) (参见@Kyouya Sato 的 [https://www.wiseowl.co.uk/blog/s2930/module-not-found-error.htm]2

and run Run Code it works and I also get并运行运行代码它可以工作,我也得到了

cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/envs/conda_envNAME/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python38.zip', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/lib-dynload', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/site-packages']

without changing不变

settings.json设置.json

it does also not work using Run Code .使用Run Code也不起作用。

Run Python File is not working at all.运行 Python 文件根本不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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