VS Code任务和Python虚拟环境

时间:2018-03-12 14:32:49

标签: python visual-studio-code

我在我的工作区设置中声明了一个python虚拟环境,一切都很好。

现在我有一个调用make目标的构建任务,后者又调用了一个pip包。当我运行它时,它不会使用我的venv,即使它是选定的解释器。

我在调用activate venv/...命令之前尝试添加make但我在嵌入式终端中遇到Permission denied错误。

如何同时使用虚拟环境和任务?

5 个答案:

答案 0 :(得分:5)

我测试了以下tasks.json并且它可以工作:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "test",
      "type": "shell",
      "command": "source /home/me/.pyenvs/myenv/bin/activate; make"
    }
  ]
}

首先激活虚拟环境,然后执行make。

答案 1 :(得分:0)

回答您的问题可能为时已晚,但是窍门是将命令字段设置为指向虚拟环境的python可执行文件,而不是默认的SELECT * FROM [MobileNumber] LEFT OUTER JOIN [MobileInventory] ON [MobileNumber].[Number] = RIGHT([MobileInventory], LEN([MobileNumber].[Number])); SELECT * FROM [MobileNumber] LEFT OUTER JOIN [MobileInventory] ON [MobileInventory] LIKE '%' + [MobileNumber].[Number]; 。如果您正确设置了python的{​​{1}},则文件中应该包含以下内容:

.vscode

在工作区中具有此配置,然后可以使用settings.json作为其 command 字段来创建类型为{ "python.pythonPath": "env\\Scripts\\python.exe", // other settings ... } 的自定义任务。

您可以在VSCode的文档this section中阅读全部内容。


此示例创建了Django process任务:

${config:python.pythonPath}

答案 2 :(得分:0)

我迟到了,但是这种选择可能有用。如果您使用pipenv代替标准venv,则可以使用pipenv run。在运行该进程之前,它将激活virtualenv。例如,这适用于构建狮身人面像:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build html",
            "type": "process",
            "command": "pipenv",
            "args": [
                "run", 
                "sphinx-build", 
                "-b", 
                "html", 
                "${workspaceFolder}", 
                "${workspaceFolder}/_build/html"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "reveal": "always",
                "panel": "new"
            }
        }
    ]
}

答案 3 :(得分:0)

您可以以适用于 tasks.jsonbash shell 的方式修改 cmd 中的 shell。这对我有用,它正确打印了虚拟环境中 python 可执行文件的完整路径。

{
    "version": "2.0.0",
    "linux": {
        "options": {
            "shell": {
                "executable": "bash",
                "args": [
                    "--init-file",
                    "env/bin/activate",
                    "-ci",
                ]
            }
        },
    },
    "windows": {
        "options": {
            "shell": {
                "executable": "cmd.exe",
                "args": [
                    "/C",
                    "env\\Scripts\\activate.bat",
                    "&"
                ]
            }
        }
    },
    "tasks": [
        {
            "label": "test shell with virtualenv",
            "command": "which python", // where on Windows for testing
            "type": "shell",
            "group": "build",
            "problemMatcher": []
        }
    ]
}

答案 4 :(得分:0)

对于可能使用 Poetry 的人来说,解决方案是运行

诗壳

在运行 VSCode 之前,但更好的解决方案是执行以下操作。这已在较早的答案中提到,但并非专门针对诗歌。

Use Poetry for Python Dependency

相关问题