目录
一. 问题
最近在 VSCode 时候Python2 环境,发现 Python Language Server 一直弹警告; 但是 Python3 环境下,无此问题;
类似于如下的方式,其实正常运行无此问题,但就是报错;
unresolved import 'unittest'
二. 环境
2.1 darwin
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
2.2 win32
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
三. 描述
看了类似问题,
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 力度如何,然后解决了再回来更新;
五. 参考
- https://code.visualstudio.com/docs/python/environments
- https://docs.python.org/zh-cn/2.7/library/sys.html?#sys.platform
- https://github.com/microsoft/python-language-server/issues/1986
- https://github.com/pypa/virtualenv/issues/355
- https://virtualenv.pypa.io/en/latest/
- https://pypi.org/project/virtualenv/
- https://pypi.org/project/virtualenv/20.4.7/
- https://pypi.org/project/virtualenv/20.0.13/
(完)