[已解决] VSCode中pylint误报Unable to import xxx (自定义模块)的问题
文章目录前言问题场景原因解决方案小结参考文献前言如题。本文给出解决方案。问题场景UbuntuVSCodeAnaconda envpylint首先,我在VSCode中打开了一个python project,然后给这个project指派了一个python环境(解释器interpreter),然后在终端pip install pylint,由于我的Python文件aaa.py中含有from module
前言
如题。本文给出解决方案。
问题场景
- Ubuntu
- VSCode
- Anaconda env
- pylint
首先,我在VSCode中打开了一个python project,然后给这个project指派了一个python环境(解释器interpreter),然后在终端pip install pylint
,由于我的Python文件aaa.py
中含有from module_xxx import method_xxx
,这个module_xxx 是自定义的模块,所以pylint直接报错说unable to import module_xxx
,而实际上这个module_xxx就和aaa.py
在一个文件夹下。
原因
不在VSCode,而在于pylint。它是静态的分析,无法找到module_xxx
,(可能是路径设定的原因),所以自然报错了。
解决方案
在该project目录下有个.vscode
文件夹,里面有settings.json
文件,原内容如下:
{
"python.pythonPath": "/home/apr/anaconda3/envs/api_misuse/bin/python"
}
修改内容如下:
{
"python.pythonPath": "/home/apr/anaconda3/envs/api_misuse/bin/python",
// "terminal.integrated.env.osx": {"PYTHONPATH": "${workspaceFolder}"},
"python.linting.pylintArgs": [
// "--disable=F0401"
"--disable=E0401"
]
}
这样就可以了,此时已经不报错了。
“强迫症”也好了。
小结
走了很多弯路,所以才在这里记。
很多失败的尝试,比如:
- 修改VSCode的setting (UI)
- 去VSCode command pallette中找linting相关的操作,
on
,off
,run linting
之类的,没用 - 看各种参考网页,越看越乱,没有人讲清楚其中原因
- 配置$PYTHONPATH. python.pythonPath
- 去找~/.pylintrc,没找到,没有。
等等。
最后找到关键词:pylint Unable to import
,然后看了几个网页,就大概懂了。
参考文献
反思:自己找文献的时候一定不能盲目找,要想清楚,想好关键字,每看一处,不要急着试,琢磨明白了再试。
- https://stackoverflow.com/questions/53653083/how-to-correctly-set-pythonpath-for-visual-studio-code 这个是主要参考。
- https://stackoverflow.com/questions/1899436/pylint-unable-to-import-error-how-to-set-pythonpath
- http://wgundamj44.github.io/post/2015-10-23-pylint-import-error-in-django/
- https://exceptionshub.com/pylint-unable-to-import-error-how-to-set-pythonpath.html
- https://github.com/Microsoft/vscode-python/issues/1185
- https://stackoverflow.com/questions/37096364/python-importerror-cannot-import-name-utils
- https://www.xspdf.com/resolution/53624143.html
- https://stackoverflow.com/questions/59702230/how-to-correctly-import-a-python-module-in-vs-code
- https://stackoverflow.com/questions/50777849/from-conda-create-requirements-txt-for-pip3
- https://code.visualstudio.com/docs/python/linting
- https://stackoverflow.com/questions/53939751/pylint-unresolved-import-error-in-visual-studio-code
- https://github.com/microsoft/python-language-server/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings
- https://github.com/microsoft/python-language-server/issues/1085#issuecomment-492919382
- https://stackoverflow.com/questions/55726180/pylint-not-running-as-expected-in-vscode
- https://www.askpython.com/python-modules/python-system-command
- https://blog.csdn.net/sinat_24527463/article/details/104254652
- https://blog.csdn.net/weixin_33716557/article/details/93275107
- https://blog.csdn.net/qq_23009105/article/details/106375341
- https://blog.csdn.net/zj010206/article/details/92806700
- https://code.visualstudio.com/docs/python/debugging
- https://stackoverflow.com/questions/55726180/pylint-not-running-as-expected-in-vscode
- https://stackoverflow.com/questions/62397982/vs-code-pylint-is-not-running
- https://stackoverflow.com/questions/50300785/vs-code-linter-pylint-not-installed-pip-install-pylint-not-working
- https://code.visualstudio.com/docs/python/linting
- https://www.jianshu.com/p/56ab5d6a95c8?utm_campaign=haruki
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)