Visual studio code 在shell中运行vscode命令与在输出中显示的命令有什么区别?
我正在vscode中使用pytest运行测试发现,输出中显示以下命令:Visual studio code 在shell中运行vscode命令与在输出中显示的命令有什么区别?,visual-studio-code,pytest,Visual Studio Code,Pytest,我正在vscode中使用pytest运行测试发现,输出中显示以下命令: python /home/dave/.vscode/extensions/ms-python.python-2020.6.88468/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir /home/dave/PythonProjects/pytest_test -s --cache-clear . 在vscode外壳中执行此命令会导致导
python /home/dave/.vscode/extensions/ms-python.python-2020.6.88468/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir /home/dave/PythonProjects/pytest_test -s --cache-clear .
在vscode外壳中执行此命令会导致导入错误
dave@dave-desktop ~/PythonProjects/pytest_test master python /home/dave/.vscode/extensions/ms-python.python-2020.6.88468/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir /home/dave/PythonProjects/pytest_test -s --cache-clear .
Traceback (most recent call last):
File "/home/dave/.vscode/extensions/ms-python.python-2020.6.88468/pythonFiles/testing_tools/run_adapter.py", line 17, in <module>
from testing_tools.adapter.__main__ import parse_args, main
File "/home/dave/.vscode/extensions/ms-python.python-2020.6.88468/pythonFiles/testing_tools/adapter/__main__.py", line 9, in <module>
from . import pytest, report
File "/home/dave/.vscode/extensions/ms-python.python-2020.6.88468/pythonFiles/testing_tools/adapter/pytest/__init__.py", line 7, in <module>
from ._discovery import discover
File "/home/dave/.vscode/extensions/ms-python.python-2020.6.88468/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 8, in <module>
import pytest
ImportError: No module named pytest
为什么这会显示错误?此命令是从另一个目录执行的。这非常令人沮丧,因为我没有机会以这种方式分析错误。您可能实际上在每个方法中调用了不同的Python解释器/环境。说: 默认情况下,Python扩展查找并使用它在系统路径中找到的第一个Python解释器 但是有几种方法可以替代这个,手动选择解释器是最明显的 为了进行调试,我建议您暂时将以下几行放在run_adapter.py的最顶端。您可能想首先备份它,因为它是VS code Python扩展的一部分:
import sys
print(f'interpreter: {sys.executable}')
这将在导入失败之前打印解释器的路径
您可能还想看看这个问题。在上面的评论中,我看不出您是否回答了虚拟环境是否已激活。VSCode可能在python可执行文件和pytest模块都存在的地方执行命令 一个选项是全局安装pytest,看看这是否有助于您以后卸载它以清理系统(如果需要) 否则,请确保在运行命令之前创建虚拟环境、激活它并在其上安装pytest
python3 -m venv /path/to/new/virtual/environment (e.g. ./venv)
./venv/bin/activate
pip3 install pytest
另外,如果Python2和Python3在同一环境中,则可能会导致冲突。我想,您能从您的示例中检查VSCode 3是否使用了正确的版本吗?您可以按Ctrl+Shift+p并选择Python:选择Intepreter以选择正确的版本。您的设置中是否有pytest路径的自定义位置?不,我没有,然后我猜虚拟环境在终端中未激活。执行pip列表并检查是否能够找到pytestpip3列表| grep-e pytest显示pytest 5.4.3.vscode/settings.json文件的输出是什么?