VS Code task and Python virtual environment

后端 未结 3 1585
一生所求
一生所求 2020-12-20 13:11

I have a python virtual environment declared in my workspace settings, everything is fine with it.

Now I have a build task that calls a make target whic

相关标签:
3条回答
  • 2020-12-20 13:46

    I'm late to the party, but this alternative might be useful. If you use pipenv in stead of standard venv, you can use pipenv run. It will activate the virtualenv before running the process. For example, this works for building sphinx:

    {
        "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"
                }
            }
        ]
    }
    
    0 讨论(0)
  • 2020-12-20 13:59

    I tested with the following tasks.json and it works:

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

    First activate virtual environment and then executes make.

    0 讨论(0)
  • 2020-12-20 14:06

    It might be a little late to answer your question but the trick is to set the command field to point to the virtual environment's python executable instead of the default python. If you set up your .vscode's settings.json correctly you should have something like this in your file:

    {
        "python.pythonPath": "env\\Scripts\\python.exe",
        // other settings ...
    }
    

    Having this config in your workspace, you can then create a custom task of type process using ${config:python.pythonPath} as it's command field.

    You can read all about it in this section of VSCode's docs.


    This example creates a Django python manage.py migrate task:

    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "Migrate",
                "type": "process",
                "command": "${config:python.pythonPath}",
                "args": [
                    "${workspaceFolder}/src/manage.py",
                    "migrate"
                ],
                "presentation": {
                    "echo": true,
                    "reveal": "always",
                    "focus": false,
                    "panel": "shared",
                    "showReuseMessage": true,
                    "clear": true
                },
                "problemMatcher": []
            }
        ]
    }
    
    0 讨论(0)
提交回复
热议问题