Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VSCode运行Python程序提示:ModuleNotFoundError,VSCode未找到导入的自定义的模块,只能手动向sys.path中把项目路径添加,但十分麻烦;Pycharm运行时会自动把项目经路添加至sys.path,开发者无需这般麻烦操作 #19499

Open
PiFrancis opened this issue Jul 19, 2022 · 35 comments
Labels
area-debugging bug Issue identified by VS Code Team member as probable bug needs PR Ready to be worked on

Comments

@PiFrancis
Copy link

Issue Type: Bug

VSCode运行Python程序提示:ModuleNotFoundError

VS Code version: Code 1.69.2 (3b889b090b5ad5793f524b5d1d39fda662b96a2a, 2022-07-18T16:12:52.460Z)
OS version: Windows_NT x64 10.0.19044
Restricted Mode: No

@vscodenpa
Copy link

感谢您创建此问题!

然而,由于您没有使用英语,我们很难进行处理。如果可能,请您将此问题描述修改为英文。请您直接编辑此问题,不要提交回复或新问题。

请注意,机器翻译服务通常无法处理用于讨论软件问题所需的技术性语言,所以我们推荐您寻找一位熟悉相关技术的人来协助翻译。

如果您无法做到,没有关系。此问题已经被标记为需要翻译,等待社区成员志愿进行翻译。


Thanks for creating this issue!

Unfortunately, as the description is not in English, it's hard for us to work on. If possible, please edit the original issue to be in English.

In our experience, automatic translation services are not able to handle the technical language needed to effectively communicate about software issues, so we recommend finding a human translator who is familiar with our technology to help.

Otherwise, we have marked this as needing translation and a community member may be able to help out.

@PiFrancis
Copy link
Author

网上同问题解决方案:通过修改项目中.vscode目录下的launch.json添加

"cwd": "${workspaceFolder}",
"env": {
    "PYTHONPATH": "${workspaceFolder}"
}

毫无帮助
借鉴文章:https://www.cnblogs.com/princessironfan/p/16452233.html

@Thepowa753

This comment was marked as abuse.

@PiFrancis
Copy link
Author

Snipaste_2022-07-19_22-38-24
Snipaste_2022-07-19_22-38-37
Snipaste_2022-07-19_22-39-08
Snipaste_2022-07-19_22-39-41

@PiFrancis
Copy link
Author

PiFrancis commented Jul 19, 2022

Please look at these images

@PiFrancis

This comment was marked as off-topic.

@Tyriar Tyriar transferred this issue from microsoft/vscode Jul 19, 2022
@Tyriar Tyriar removed their assignment Jul 19, 2022
@kam193
Copy link

kam193 commented Jul 19, 2022

Check: utils/__init__.py
Look for: "importing modules" guide

Try from utils.create_tree... -> from .utils.create_tree...
Look for: "relative imports" guide.

This is not an issue with the extension, it's just an issue in the provided code.

@PiFrancis
Copy link
Author

Check: Look for: "importing modules" guideutils/__init__.py

Try -> Look for: "relative imports" guide.from utils.create_tree... ``from .utils.create_tree...

This is not an issue with the extension, it's just an issue in the provided code.

image

@karthiknadig karthiknadig added the triage-needed Needs assignment to the proper sub-team label Jul 20, 2022
@karthiknadig
Copy link
Member

@PCoeur Can you share the file layout? and what is the path to the file that you are executing?
Please also print out sys.path from both scenarios.

@karthiknadig karthiknadig self-assigned this Jul 20, 2022
@karthiknadig karthiknadig added the info-needed Issue requires more information from poster label Jul 20, 2022
@karthiknadig karthiknadig added bug Issue identified by VS Code Team member as probable bug area-debugging labels Jul 20, 2022
@PiFrancis
Copy link
Author

@PCoeur Can you share the file layout? and what is the path to the file that you are executing? Please also print out sys.path from both scenarios.

image
Developers need to manually add the project path to PYTHONPATH to solve the problem before importing the custom modules

@paulacamargo25 paulacamargo25 removed info-needed Issue requires more information from poster triage-needed Needs assignment to the proper sub-team labels Sep 15, 2022
@paulacamargo25 paulacamargo25 added the needs PR Ready to be worked on label Sep 15, 2022
@paulacamargo25 paulacamargo25 removed their assignment Sep 15, 2022
@geekmister
Copy link

@PCoeur 看这个issue的时间是2022-09月的,时间挺久的了,你解决了吗?我已经被这个issue消耗掉了3个小时:sob:,vscode相比于Pycharm易用性对开发者不太友好,我是换vscode才几天的时间。

@PiFrancis
Copy link
Author

PiFrancis commented Feb 5, 2023

@PCoeur 看这个issue的时间是2022-09月的,时间挺久的了,你解决了吗?我已经被这个issue消耗掉了3个小时😭,vscode相比于Pycharm易用性对开发者不太友好,我是换vscode才几天的时间。

vscode的settings.json中添加以下内容即可:

// 解决Python导入自定义模块运行程序时无法找到模块,由于运行程序时未将项目路径添加值PYTHONPATH
// macOS
"terminal.integrated.env.osx": {
    "PYTHONPATH": "${workspaceFolder}/",
},
// Linux
"terminal.integrated.env.linux": {
    "PYTHONPATH": "${workspaceFolder}/",
},
// Windows
"terminal.integrated.env.windows": {
    "PYTHONPATH": "${workspaceFolder}/",
}

@geekmister
Copy link

@PCoeur 看这个issue的时间是2022-09月的,时间挺久的了,你解决了吗?我已经被这个issue消耗掉了3个小时😭,vscode相比于Pycharm易用性对开发者不太友好,我是换vscode才几天的时间。

vscode的settings.json中添加以下内容即可:

// 解决Python导入自定义模块运行程序时无法找到模块,由于运行程序时未将项目路径添加值PYTHONPATH
"terminal.integrated.env.osx": {
    "PYTHONPATH": "${workspaceFolder}/",
},
"terminal.integrated.env.linux": {
    "PYTHONPATH": "${workspaceFolder}/",
},
"terminal.integrated.env.windows": {
    "PYTHONPATH": "${workspaceFolder}/",
}

image
添加配置后依然 NotFoundModule, 不过还是感谢你提供帮助 😊

@PiFrancis
Copy link
Author

PiFrancis commented Feb 5, 2023

@PCoeur 看这个issue的时间是2022-09月的,时间挺久的了,你解决了吗?我已经被这个issue消耗掉了3个小时😭,vscode相比于Pycharm易用性对开发者不太友好,我是换vscode才几天的时间。

vscode的settings.json中添加以下内容即可:

// 解决Python导入自定义模块运行程序时无法找到模块,由于运行程序时未将项目路径添加值PYTHONPATH
"terminal.integrated.env.osx": {
    "PYTHONPATH": "${workspaceFolder}/",
},
"terminal.integrated.env.linux": {
    "PYTHONPATH": "${workspaceFolder}/",
},
"terminal.integrated.env.windows": {
    "PYTHONPATH": "${workspaceFolder}/",
}

image 添加配置后依然 NotFoundModule, 不过还是感谢你提供帮助 😊

macOS系统?
image

@geekmister
Copy link

@PCoeur 看这个issue的时间是2022-09月的,时间挺久的了,你解决了吗?我已经被这个issue消耗掉了3个小时😭,vscode相比于Pycharm易用性对开发者不太友好,我是换vscode才几天的时间。

vscode的settings.json中添加以下内容即可:

// 解决Python导入自定义模块运行程序时无法找到模块,由于运行程序时未将项目路径添加值PYTHONPATH
"terminal.integrated.env.osx": {
    "PYTHONPATH": "${workspaceFolder}/",
},
"terminal.integrated.env.linux": {
    "PYTHONPATH": "${workspaceFolder}/",
},
"terminal.integrated.env.windows": {
    "PYTHONPATH": "${workspaceFolder}/",
}

image 添加配置后依然 NotFoundModule, 不过还是感谢你提供帮助 😊

macOS系统?

是的,MacOS...

@PiFrancis
Copy link
Author

@PCoeur 看这个issue的时间是2022-09月的,时间挺久的了,你解决了吗?我已经被这个issue消耗掉了3个小时😭,vscode相比于Pycharm易用性对开发者不太友好,我是换vscode才几天的时间。

vscode的settings.json中添加以下内容即可:

// 解决Python导入自定义模块运行程序时无法找到模块,由于运行程序时未将项目路径添加值PYTHONPATH
"terminal.integrated.env.osx": {
    "PYTHONPATH": "${workspaceFolder}/",
},
"terminal.integrated.env.linux": {
    "PYTHONPATH": "${workspaceFolder}/",
},
"terminal.integrated.env.windows": {
    "PYTHONPATH": "${workspaceFolder}/",
}

image 添加配置后依然 NotFoundModule, 不过还是感谢你提供帮助 😊

macOS系统?

是的,MacOS...

image

vscode的settings.json,项目的settings.json没试过

@geekmister
Copy link

@PCoeur 感谢你的帮助,但是尝尽各种方法还是未解决,我换回Pycharm了,先解决工作的问题了,以后有时间再慢慢研究:smile:

@PiFrancis
Copy link
Author

@PCoeur 感谢你的帮助,但是尝尽各种方法还是未解决,我换回Pycharm了,先解决工作的问题了,以后有时间再慢慢研究smile

OK

@qiuqiao
Copy link

qiuqiao commented Nov 17, 2023

谢谢,你提供的方法很有用

@PiFrancis
Copy link
Author

谢谢,你提供的方法很有用

不客气

@fskz
Copy link

fskz commented Dec 9, 2023

目前看到的所有方案试了下都无效😭,标记下继续研究了

@PiFrancis
Copy link
Author

目前看到的所有方案试了下都无效😭,标记下继续研究了

展开说说

@fskz
Copy link

fskz commented Dec 9, 2023

目前看到的所有方案试了下都无效😭,标记下继续研究了

展开说说

发现问题了,之前因为没把改配置之前的bash终端关掉,导致代码一直运行在旧的bash终端上,新增的配置没生效,现在把vscode右下角的bash全关了,重新运行就可以了

@zPz-002
Copy link

zPz-002 commented Mar 6, 2024

试了一圈让配置项目里边.vscode/lanuch.json的都不行,你这个方法一下就成了,强👍

@Allin2000
Copy link

@PCoeur 看这个issue的时间是2022-09月的,时间挺久的了,你解决了吗?我已经被这个issue消耗掉了3个小时😭,vscode相比于Pycharm易用性对开发者不太友好,我是换vscode才几天的时间。

vscode的settings.json中添加以下内容即可:

// 解决Python导入自定义模块运行程序时无法找到模块,由于运行程序时未将项目路径添加值PYTHONPATH
// macOS
"terminal.integrated.env.osx": {
    "PYTHONPATH": "${workspaceFolder}/",
},
// Linux
"terminal.integrated.env.linux": {
    "PYTHONPATH": "${workspaceFolder}/",
},
// Windows
"terminal.integrated.env.windows": {
    "PYTHONPATH": "${workspaceFolder}/",
}

this is true.但是你设置后,需要重新打开项目或者命令行,才能生效。 不过这个是为什么错误呢?

@geekmister
Copy link

在项目根目录创建一个.venv就可以解决了,可以实现Pycharm的自动导包效果!

@PiFrancis
Copy link
Author

PiFrancis commented May 22, 2024

在项目根目录创建一个.venv就可以解决了,可以实现Pycharm的自动导包效果!

.venv空隐藏文件?这种方案只针对当前项目有用,其他项目还需要这样搞有点麻烦

@geekmister
Copy link

在项目根目录创建一个.venv就可以解决了,可以实现Pycharm的自动导包效果!

.venv空隐藏文件?这种方案只针对当前项目有用,其他项目还需要这样搞有点麻烦

crtil + shift + p 在当前项目根目录下创建虚拟环境目录.venv

@PiFrancis
Copy link
Author

@PCoeur 看这个issue的时间是2022-09月的,时间挺久的了,你解决了吗?我已经被这个issue消耗掉了3个小时😭,vscode相比于Pycharm易用性对开发者不太友好,我是换vscode才几天的时间。

vscode的settings.json中添加以下内容即可:

// 解决Python导入自定义模块运行程序时无法找到模块,由于运行程序时未将项目路径添加值PYTHONPATH
// macOS
"terminal.integrated.env.osx": {
    "PYTHONPATH": "${workspaceFolder}/",
},
// Linux
"terminal.integrated.env.linux": {
    "PYTHONPATH": "${workspaceFolder}/",
},
// Windows
"terminal.integrated.env.windows": {
    "PYTHONPATH": "${workspaceFolder}/",
}

this is true.但是你设置后,需要重新打开项目或者命令行,才能生效。 不过这个是为什么错误呢?

添加的配置没生效,可以重新加载窗口(Win: ctrl + shift + p快捷键)

@PiFrancis
Copy link
Author

在项目根目录创建一个.venv就可以解决了,可以实现Pycharm的自动导包效果!

.venv空隐藏文件?这种方案只针对当前项目有用,其他项目还需要这样搞有点麻烦

crtil + shift + p 在当前项目根目录下创建虚拟环境目录.venv

虚拟环境也是可以,前面本人提供的那种解决方案主要解决不创建虚拟环境场景

@yebei199
Copy link

@PCoeur 看这个issue的时间是2022-09月的,时间挺久的了,你解决了吗?我已经被这个issue消耗掉了3个小时😭,vscode相比于Pycharm易用性对开发者不太友好,我是换vscode才几天的时间。

vscode的settings.json中添加以下内容即可:

// 解决Python导入自定义模块运行程序时无法找到模块,由于运行程序时未将项目路径添加值PYTHONPATH
// macOS
"terminal.integrated.env.osx": {
    "PYTHONPATH": "${workspaceFolder}/",
},
// Linux
"terminal.integrated.env.linux": {
    "PYTHONPATH": "${workspaceFolder}/",
},
// Windows
"terminal.integrated.env.windows": {
    "PYTHONPATH": "${workspaceFolder}/",
}

非常感谢, 已经解决了, 之前是每次都要自己手动打全部路径加上 -m, 感觉天都塌了

@xx025
Copy link

xx025 commented Sep 16, 2024

@PCoeur 看这个issue的时间是2022-09月的,时间挺久的了,你解决了吗?我已经被这个issue消耗掉了3个小时😭,vscode相比于Pycharm易用性对开发者不太友好,我是换vscode才几天的时间。

vscode的settings.json中添加以下内容即可:

// 解决Python导入自定义模块运行程序时无法找到模块,由于运行程序时未将项目路径添加值PYTHONPATH
// macOS
"terminal.integrated.env.osx": {
    "PYTHONPATH": "${workspaceFolder}/",
},
// Linux
"terminal.integrated.env.linux": {
    "PYTHONPATH": "${workspaceFolder}/",
},
// Windows
"terminal.integrated.env.windows": {
    "PYTHONPATH": "${workspaceFolder}/",
}

正确的解决方案,需要重新打开新的Terminal 窗口,运行正常

@zxjhellow2
Copy link

@PCoeur 看这个issue的时间是2022-09月的,时间挺久的了,你解决了吗?我已经被这个issue消耗掉了3个小时😭,vscode相比于Pycharm易用性对开发者不太友好,我是换vscode才几天的时间。

vscode的settings.json中添加以下内容即可:

// 解决Python导入自定义模块运行程序时无法找到模块,由于运行程序时未将项目路径添加值PYTHONPATH
"terminal.integrated.env.osx": {
    "PYTHONPATH": "${workspaceFolder}/",
},
"terminal.integrated.env.linux": {
    "PYTHONPATH": "${workspaceFolder}/",
},
"terminal.integrated.env.windows": {
    "PYTHONPATH": "${workspaceFolder}/",
}

image 添加配置后依然 NotFoundModule, 不过还是感谢你提供帮助 😊

macOS系统?

是的,MacOS...

image

vscode的settings.json,项目的settings.json没试过

你好,setting.json是从哪里打开?有好多个settings.json

@PiFrancis
Copy link
Author

@PCoeur 看这个issue的时间是2022-09月的,时间挺久的了,你解决了吗?我已经被这个issue消耗掉了3个小时😭,vscode相比于Pycharm易用性对开发者不太友好,我是换vscode才几天的时间。

vscode的settings.json中添加以下内容即可:

// 解决Python导入自定义模块运行程序时无法找到模块,由于运行程序时未将项目路径添加值PYTHONPATH
"terminal.integrated.env.osx": {
    "PYTHONPATH": "${workspaceFolder}/",
},
"terminal.integrated.env.linux": {
    "PYTHONPATH": "${workspaceFolder}/",
},
"terminal.integrated.env.windows": {
    "PYTHONPATH": "${workspaceFolder}/",
}

image 添加配置后依然 NotFoundModule, 不过还是感谢你提供帮助 😊

macOS系统?

是的,MacOS...

image vscode的settings.json,项目的settings.json没试过

你好,setting.json是从哪里打开?有好多个settings.json

Image

@zxjhellow2
Copy link

@PCoeur 看这个issue的时间是2022-09月的,时间挺久的了,你解决了吗?我已经被这个issue消耗掉了3个小时😭,vscode相比于Pycharm易用性对开发者不太友好,我是换vscode才几天的时间。

vscode的settings.json中添加以下内容即可:

// 解决Python导入自定义模块运行程序时无法找到模块,由于运行程序时未将项目路径添加值PYTHONPATH
"terminal.integrated.env.osx": {
    "PYTHONPATH": "${workspaceFolder}/",
},
"terminal.integrated.env.linux": {
    "PYTHONPATH": "${workspaceFolder}/",
},
"terminal.integrated.env.windows": {
    "PYTHONPATH": "${workspaceFolder}/",
}

image 添加配置后依然 NotFoundModule, 不过还是感谢你提供帮助 😊

macOS系统?

是的,MacOS...

image vscode的settings.json,项目的settings.json没试过

你好,setting.json是从哪里打开?有好多个settings.json

Image

谢谢,尝试添加了Linux的,然后也重启了vscode,但是好像没啥用,这个真鸡肋啊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-debugging bug Issue identified by VS Code Team member as probable bug needs PR Ready to be worked on
Projects
None yet
Development

No branches or pull requests