<p>您至少有两个不同的问题:</p>
<ol>
<li>在第三个图像中,您正试图使用不在virtualenv中的python二进制文件执行python脚本</李>
<li>VS代码可能未正确配置以使用virtualenv的python解释器</李>
</ol>
<p>编辑:如果第三个图像是VS代码终端的屏幕截图,而不是外部cmd,并且假设您正在编辑器工具中运行Python代码,则它可能是2</p>
<p>如果是这种情况,请检查VS代码中的<code>launch.json</code>文件。应该是这样的:</p>
<pre class="lang-json prettyprint-override"><code>{
"version": "0.1.0",
"configurations": [
{
"name": "Python",
"type": "python",
"request": "launch",
"python": "<path to your virtualenv>/bin/python",
"program": "${fileBasename}",
}
]
}
</code></pre>
<p>确保您的<code>"python"</code>指向正确的python二进制文件,也就是您的virtualenv路径中的那个</p>
<p>然后在VS代码的常规设置中,可以将<code>"venvFolders"</code>设置为定义虚拟环境的路径列表</p>
<pre class="lang-json prettyprint-override"><code>{
...
"python.venvFolders:" ["<venv path 1>", "<venv path 2>"]
}
</code></pre>
<p>查看<a href="https://code.visualstudio.com/docs/python/environments#_where-the-extension-looks-for-environments" rel="nofollow noreferrer">the official guide:</a>和另一个与<a href="https://code.visualstudio.com/docs/python/settings-reference#_general-settings" rel="nofollow noreferrer">general settings</a>相关的</p>