如何在vs代码中定义虚拟环境

时间:2020-10-22 09:54:20

标签: python django vscode-settings

我是vs代码的菜鸟(昨天开始使用它,我喜欢它)

我有点不适。我正在执行django项目,并且正在将virtualenv与virtualenvwrapper一起使用。我的问题是(例如)该行中的

from django.shortcuts import render

短毛猫说

无法导入'django.shortcuts'pylint(导入错误)

我的虚拟环境位于目录〜/ Envs

ls -l Envs

返回

drwxr-xr-x 4 jeff jeff 4096 Jun 18 14:46 django
-rwxr-xr-x 1 jeff jeff  135 Jun 18 15:23 get_env_details
-rw-r--r-- 1 jeff jeff   96 Jun 18 15:23 initialize
-rw-r--r-- 1 jeff jeff   73 Jun 18 15:23 postactivate
-rw-r--r-- 1 jeff jeff   75 Jun 18 15:23 postdeactivate
-rwxr-xr-x 1 jeff jeff   66 Jun 18 15:23 postmkproject
-rw-r--r-- 1 jeff jeff   73 Jun 18 15:23 postmkvirtualenv
-rwxr-xr-x 1 jeff jeff  110 Jun 18 15:23 postrmvirtualenv
-rwxr-xr-x 1 jeff jeff   99 Jun 18 15:23 preactivate
-rw-r--r-- 1 jeff jeff   76 Jun 18 15:23 predeactivate
-rwxr-xr-x 1 jeff jeff   91 Jun 18 15:23 premkproject
-rwxr-xr-x 1 jeff jeff  130 Jun 18 15:23 premkvirtualenv
-rwxr-xr-x 1 jeff jeff  111 Jun 18 15:23 prermvirtualenv
drwxr-xr-x 4 jeff jeff 4096 Jun 18 15:26 wagtail

我正在venv django

我尝试将 Python:Venv路径和/或 Python:Venv文件夹设置为〜/ Envs 〜/ Envs / django 。我已经重新启动vs-code,但是问题仍然存在

我该怎么办?

2 个答案:

答案 0 :(得分:1)

您是否尝试过这样的操作: 要选择特定的环境,请使用Python:从命令面板(Ctrl + Shift + P)中选择“解释器”命令。

enter image description here

您可以随时切换环境;切换环境可帮助您根据需要使用不同的解释器或库版本来测试项目的不同部分。

“ Python:选择解释器”命令显示可用全局环境,conda环境和虚拟环境的列表。 (有关详细信息,请参见“在扩展程序的环境中查找”部分,其中包括这些类型的环境之间的区别。)例如,下图显示了几个Anaconda和CPython安装以及一个conda环境和一个位于的虚拟环境(env)。在工作区文件夹中:

enter image description here

来源: https://code.visualstudio.com/docs/python/environments

答案 1 :(得分:0)

尝试使用venv将Python Interpreter设置为该选项。 enter image description here