最近在 VSCode 时候Python2 环境,发现 Python Language Server 一直弹警告; 但是 Python3 环境下,无此问题;
类似于如下的方式,其实正常运行无此问题,但就是报错;
unresolved import 'unittest'
目前升级到如下环境版本:
版本: 1.57.1 (user setup)
提交: 507ce72a4466fbb27b715c3722558bb15afa9f48
日期: 2021-06-17T13:28:07.755Z
此问题不存在;
.env 搜索路径问题?
这个问题配置情况,网上搜的各种问题都有,反馈吐槽此类问题失效的人不少,有绝对路径和相对路径区分,提供的不靠谱,同时再不同平台上起的效果还不一样;
怀疑系统环境变量有问题?
理论情况:
VSCode 环境变量 > 系统环境变量
实际情况:
VSCode 环境变量 < 系统环境变量
Mac OS X:
版本: 1.56.2 (Universal)
提交: 054a9295330880ed74ceaedda236253b4f39a335
日期: 2021-05-12T17:44:30.902Z
Electron: 12.0.4
Chrome: 89.0.4389.114
Node.js: 14.16.0
V8: 8.9.255.24-electron.0
OS: Darwin x64 19.4.0
# Python 2
Python: 2.7.17
pip: 19.3.1
virtualenv: 20.4.6
# Python 3
Python: 3.8.5
pip: 21.0.1
virtualenv: 20.4.6
Windows:
版本: 1.56.2 (user setup)
提交: 054a9295330880ed74ceaedda236253b4f39a335
日期: 2021-05-12T17:13:13.157Z
Electron: 12.0.4
Chrome: 89.0.4389.114
Node.js: 14.16.0
V8: 8.9.255.24-electron.0
OS: Windows_NT x64 10.0.18363
# Python 2
Python: 2.7.16
pip: 18.1
virtualenv: 20.0.13
# Python 3
Python: 3.8.2
pip: 19.2.3
virtualenv: 20.4.7
Mac OS X:
pass
Windows:
版本: 1.57.1 (user setup)
提交: 507ce72a4466fbb27b715c3722558bb15afa9f48
日期: 2021-06-17T13:28:07.755Z
Electron: 12.0.7
Chrome: 89.0.4389.128
Node.js: 14.16.0
V8: 8.9.255.25-electron.0
OS: Windows_NT x64 10.0.19042
看了类似问题,
1. https://github.com/microsoft/python-language-server/issues/1986
2. https://github.com/pypa/virtualenv/issues/355
发现有人说可能是 virtualenv 的问题,那我就试着装一下旧版本 virtualenv 看看问题会不会出现;
目前,Windows 上,Python2 和 Python3 无此问题,Mac 上只有Python 2 有问题,但是 Python 3 无此问题;
先卸载 virtualenv 新版本
$ pip2 uninstall virtualenv
然后安装旧版本 virtualenv
$ pip2 install virtualenv==20.0.13
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting virtualenv==20.0.13
Downloading https://files.pythonhosted.org/packages/84/3a/9d656ec2535fa5f6680d55ef93a05f890bd1b2ad6f2bf97b34a679abf365/virtualenv-20.0.13-py2.py3-none-any.whl (4.6MB)
|████████████████████████████████| 4.6MB 61kB/s
Requirement already satisfied: six<2,>=1.9.0 in /usr/local/lib/python2.7/site-packages (from virtualenv==20.0.13) (1.16.0)
Requirement already satisfied: filelock<4,>=3.0.0 in /usr/local/lib/python2.7/site-packages (from virtualenv==20.0.13) (3.0.12)
Collecting importlib-metadata<2,>=0.12; python_version < "3.8"
Downloading https://files.pythonhosted.org/packages/8e/58/cdea07eb51fc2b906db0968a94700866fc46249bdc75cac23f9d13168929/importlib_metadata-1.7.0-py2.py3-none-any.whl
Requirement already satisfied: distlib<1,>=0.3.0 in /usr/local/lib/python2.7/site-packages (from virtualenv==20.0.13) (0.3.1)
Requirement already satisfied: appdirs<2,>=1.4.3 in /usr/local/lib/python2.7/site-packages (from virtualenv==20.0.13) (1.4.4)
Collecting importlib-resources<2,>=1.0; python_version < "3.7"
Downloading https://files.pythonhosted.org/packages/7f/2d/88f166bcaadc09d9fdbf1c336ad118e01b7fe1155e15675e125be2ff1899/importlib_resources-1.5.0-py2.py3-none-any.whl
Requirement already satisfied: contextlib2<1,>=0.6.0; python_version < "3.3" in /usr/local/lib/python2.7/site-packages (from virtualenv==20.0.13) (0.6.0.post1)
Requirement already satisfied: pathlib2<3,>=2.3.3; python_version < "3.4" and sys_platform != "win32" in /usr/local/lib/python2.7/site-packages (from virtualenv==20.0.13) (2.3.5)
Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python2.7/site-packages (from importlib-metadata<2,>=0.12; python_version < "3.8"->virtualenv==20.0.13) (1.2.0)
Requirement already satisfied: configparser>=3.5; python_version < "3" in /usr/local/lib/python2.7/site-packages (from importlib-metadata<2,>=0.12; python_version < "3.8"->virtualenv==20.0.13) (4.0.2)
Requirement already satisfied: singledispatch; python_version < "3.4" in /usr/local/lib/python2.7/site-packages (from importlib-resources<2,>=1.0; python_version < "3.7"->virtualenv==20.0.13) (3.6.1)
Requirement already satisfied: typing; python_version < "3.5" in /usr/local/lib/python2.7/site-packages (from importlib-resources<2,>=1.0; python_version < "3.7"->virtualenv==20.0.13) (3.10.0.0)
Requirement already satisfied: scandir; python_version < "3.5" in /usr/local/lib/python2.7/site-packages (from pathlib2<3,>=2.3.3; python_version < "3.4" and sys_platform != "win32"->virtualenv==20.0.13) (1.10.0)
Installing collected packages: importlib-metadata, importlib-resources, virtualenv
Found existing installation: importlib-metadata 2.1.1
Uninstalling importlib-metadata-2.1.1:
Successfully uninstalled importlib-metadata-2.1.1
Found existing installation: importlib-resources 3.3.1
Uninstalling importlib-resources-3.3.1:
Successfully uninstalled importlib-resources-3.3.1
Successfully installed importlib-metadata-1.7.0 importlib-resources-1.5.0 virtualenv-20.0.13
此时使用 virtualenv 工具重新创建 venv2 ,用 VSCode 打开问题依旧;
此问题尚未解决,后续看 VSCode 支持 Python2 力度如何,然后解决了再回来更新;
此问题更新版本后,不存在,后续不再维护此问题;
Windows 平台下,【Python 2.7 ,Python 3.x 】版本,
快捷键:Ctrl + Shift + U,输出 -> Python | Python 语言服务器
具体版本信息如下:
版本: 1.57.1 (user setup)
提交: 507ce72a4466fbb27b715c3722558bb15afa9f48
日期: 2021-06-17T13:28:07.755Z
Electron: 12.0.7
Chrome: 89.0.4389.128
Node.js: 14.16.0
V8: 8.9.255.25-electron.0
OS: Windows_NT x64 10.0.19042
Python 解释器搜索路径如下:
# Python 解释器
Python 解释器路径: C:\WorkSpace\...\Python-DeMo\tests
C:\WINDOWS\SYSTEM32\python27.zip
C:\Python\Python27\DLLs
C:\Python\Python27\lib
C:\Python\Python27\lib\plat-win
C:\Python\Python27\lib\lib-tk
c:\WorkSpace\...\Python-DeMo\venv2\Scripts
c:\WorkSpace\...\Python-DeMo\venv2
c:\WorkSpace\...\Python-DeMo\venv2\lib\site-packages
c:\workspace\...\python-demo\src
c:\WorkSpace\...\Python-DeMo\venv2\Scripts\python.exe
Python 语言服务器搜索路径如下:
# Python 语言服务器
Search paths for c:\WorkSpace\...\Python-DeMo
c:\Users\...\.vscode\extensions\ms-python.vscode-pylance-2021.6.3\dist\typeshed-fallback\stdlib
c:\WorkSpace\...\Python-DeMo
c:\WorkSpace\...\Python-DeMo\src
C:\WorkSpace\...\Python-DeMo\tests
c:\WorkSpace\...\Python-DeMo\typings
c:\Users\...\.vscode\extensions\ms-python.vscode-pylance-2021.6.3\dist\typeshed-fallback\stubs\...
c:\Users\...\.vscode\extensions\ms-python.vscode-pylance-2021.6.3\dist\bundled\stubs
[Info] Searching for source files
[Info] Auto-excluding c:\WorkSpace\...\Python-DeMo\venv
[Info] Auto-excluding c:\WorkSpace\...\Python-DeMo\venv2
[Info] Found 73 source files
[Info] Background analysis(1) root directory: c:\Users\...\.vscode\extensions\ms-python.vscode-pylance-2021.6.3\dist
[Info] Background analysis(1) started
此时无需提供PYTHONPATH 环境变量,和${workspaceFolder}/.env,和python.analysis.extraPaths,和python.autoComplete.extraPaths,说明如下:
python.analysis.extraPaths 为较新版本采用,
python.autoComplete.extraPaths已弃用;两者都是Python 语言服务器所采用的扩展路径加载解析器;
结论如下:
1. Windows 平台下,VSCode 中的${workspaceFolder}/.env 指向的环境变量文件不支持
相对路径,必须绝对路径才受支持;
2. VSCode 的一个 Bug;
(完)