Question
How do I get a vscode task's command to run in a python virtual environment/shell created by another task?
Example
I'm trying to run a couple of tasks in sequence using vscode.
The first task will run the command pipenv shell.
The second task needs to be run in the virtual environment created by the first task.
The second task will only run if I exit the virtual environment created by the first task manually by running exit in the created shell.
Presumably because it is waiting for the first task to complete - but the first task is only complete when the process ends (when I run exit).
tasks.json
{
"tasks": [
{
"label": "first task",
"type": "shell",
"command": "pipenv shell",
"presentation": {
"panel": "shared",
},
},
{
"label": "second task",
"type": "shell",
"command": "<some_command>",
"presentation": {
"panel": "shared",
},
},
{
"label": "combine tasks",
"type": "shell",
"command": "echo hello",
"dependsOrder": "sequence",
"dependsOn": ["first task", "second task"],
},
],
}
The first task to do is activate the virtual environment for the correct terminal you use (Powershell, cmd, bash, ....)
Then run the additional terminal tasks sequentially.
I used Powershell and Python venv for virtual environment
I used these tasks (no terminal was active) and they run in the terminal variant with the > Executing task, Terminal will be reused by tasks texts.
{
"label": "Activate python venv",
"type": "shell",
"command": "c:/Project/Python/.venv/Scripts/Activate.ps1",
"problemMatcher": []
},
{
"label": "pip list",
"type": "shell",
"command": "pip list",
"problemMatcher": []
},
{
"label": "combine Python",
"type": "shell",
"dependsOrder": "sequence",
"dependsOn": ["Activate python venv", "pip list"],
"problemMatcher": []
}
It will list all the packages installed in the virtual environment.
Related
Try to debug the Python code in Container using VS Code Remote Container Extension.
After the docker build task, I got a "Debug Adapter Executable not provide" error. Where could be wrong?
launch.json
{
"configurations": [
{
"name": "Docker: Python - General",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"projectType": "general"
}
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "docker-build",
"label": "docker-build",
"platform": "python",
"dockerBuild": {
"tag": "py:latest",
"dockerfile": "${workspaceFolder}/Dockerfile",
"context": "${workspaceFolder}",
"pull": true
}
},
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": [
"docker-build"
],
"python": {
"file": "app.py"
}
}
]
}
Looks like it is resolved if you define a local python interpreter even if you want to use docker. Open a .py file and define it with the button right bottom.
I also encountered this problem. After trying a fresh install of the VS Code Docker extension, to no avail, I noticed the actually command being triggered by the "docker-run: debug" task in the tasks.json:
docker run -dt -P --name "loadtiktokcampaigns-dev" --label
"com.microsoft.created-by=visual-studio-code" -v
"/Users/tnightengale/.vscode/extensions/ms-python.python-
2022.12.0/pythonFiles/lib/python/debugpy:/debugpy:ro,z"
--entrypoint "python3" "loadtiktokcampaigns:latest"
The key here is the -v command that is attempting to mount the /debugpy files into the container from the VS Code Python extension files.
For some reason, those files were not actually located at that path on my Mac. Deleting the ms-python.python-2022.12.0/ folder and reinstalling the VS Code Python extension solved the issue for me.
In Visual Studio Code I changed the Default Terminal to Command Prompt.
When I want to debug my Azure Function the following command is executed:
Executing task: .venv\Scripts\activate ; func host start <
However, I think it should put the cmd separator "&&" instead of ";".
Otherwise the func host start will not execute.
Executing task: .venv\Scripts\activate && func host start <
As far as I know it should auto-detect this.
Is there a way to manually change this?
My settings.json:
{
"terminal.integrated.automationShell.windows": "C:\\windows\\System32\\cmd.exe",
"terminal.integrated.defaultProfile.windows": "Command Prompt",
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell"
},
"Command Prompt": {
"path": [
"${env:windir}\\Sysnative\\cmd.exe",
"${env:windir}\\System32\\cmd.exe"
],
"args": [],
"icon": "terminal-cmd"
},
"Git Bash": {
"source": "Git Bash"
},
"C:\\windows\\System32\\cmd.exe": {
"path": "C:\\windows\\System32\\cmd.exe",
"args": [],
"icon": "terminal-cmd"
},
"JavaScript Debug Terminal": {
"extensionIdentifier": "ms-vscode.js-debug",
"icon": "debug",
"id": "extension.js-debug.debugTerminal",
"title": "JavaScript Debug Terminal"
}
}
}
Thanks in advance!
#########
SOLUTION:
I was able the change the separator by editing the tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "cmd host start",
"type": "shell",
"dependsOn": "pip install (functions)",
"windows": {
"command": "${config:azureFunctions.pythonVenv}\\Scripts\\activate && func host start"
},
"isBackground": true,
"problemMatcher": "$func-python-watch",
},
{
"label": "pip install (functions)",
"type": "shell",
"osx": {
"command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
},
"windows": {
"command": "${config:azureFunctions.pythonVenv}\\Scripts\\python -m pip install -r requirements.txt"
},
"linux": {
"command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
},
"problemMatcher": []
}
]
}
Afterwards in the launch.json, I set the preLaunchTask to "cmd host start".
Thank you ejizba. Posting your suggestion as an answer to help other community members.
"The debug configuration is specified in your tasks.json and launch.json files in the .vscode folder. The 'windows' 'command' properties default to a PowerShell separator (;), but you're welcome to switch it to a cmd separator (I believe that would just be &&) .
however, because I think we can potentially refactor the tasks.json so that it doesn't use terminal-specific separators and prevent similar issues in the future ."
For more information please refer this SO THREAD
Environment
I have a Python quickstart in VS Code as part of a multi-root workspace. I have manually added 3 files; tasks.json, settings.json and launch.json.
Unexpected behaviour
When i debug the project, the main script fails because of missing modules. This is because on start-up the modules listed in requirements.txt are not being installed.
Tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "pipInstall",
"type": "shell",
"osx": {
"command": "${config:python.pythonPath}/bin/python -m pip install -r requirements.txt"
},
"windows": {
"command": "${config:python.pythonPath}\\Scripts\\python -m pip install -r requirements.txt"
},
"linux": {
"command": "${config:python.pythonPath}/bin/python -m pip install -r requirements.txt"
},
"problemMatcher": [],
"options": {
"cwd": "${workspaceFolder}"
}
}
]
}
Launch.json
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\batch-python-quickstart\\src\\python_quickstart_client.py",
"console": "integratedTerminal"
}
]
}
Settings.json
{
"python.pythonPath": "C:\\Python Virtual Environments\\myenvid\\.venv\\Scripts\\python.exe"
}
Why the modules were not installed
The modules specified in the requirement.txt file were not being installed because there was no reference to the tasks.json in the launch.json. To execute the tasks before executing the python app requires "preLaunchTask": "pipInstall" to reference the task named pipInstall in tasks.json.
Code after changes
Note: I also fixed some incorrect paths and moved my virtual environments to a new directory outside my projects.
Settings.json
{
"python.pythonPath": "C:\\thepath\\.venv\\Scripts\\python.exe"
}
Launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\batch-python-quickstart\\src\\python_quickstart_client.py",
"console": "integratedTerminal",
"preLaunchTask": "pipInstall"
}
]
}
Tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "pipInstall",
"type": "shell",
"osx": {
"command": "${config:python.pythonPath}/bin/python -m pip install -r requirements.txt"
},
"windows": {
"command": "${config:python.pythonPath} -m pip install -r requirements.txt"
},
"linux": {
"command": "${config:python.pythonPath}/bin/python -m pip install -r requirements.txt"
},
"problemMatcher": [],
"options": {
"cwd": "${workspaceFolder}\\batch-python-quickstart\\src"
}
}
]
}
Furthermore
It is possible to cascade tasks by adding the same dependency property inside a task; "dependsOn": "othertasklabel". Ideal for a set of build steps.
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 which in turn calls a pip package. When I run it, it doesn't use my venv even though it is the selected interpreter.
I've tried to add a activate venv/... before calling my make command but I get a Permission denied error in the embedded terminal.
How can I use a virtual environment and tasks at the same time?
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": []
}
]
}
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.
I had quite a lot of trouble with this recently myself. I wanted to update package requirements before running code, but I didn't want to implement a complex or maintenance-heavy solution. The following works well for me across Windows, OSX, and Linux environments.
{
"version": "2.0.0",
"tasks": [
{
"label": "pipInstall",
"type": "shell",
"command": "${command:python.interpreterPath}",
"args": ["-m", "pip", "install", "-r", "requirements.txt"]
}
]
}
I believe ${command:python.interpreterPath} points to whatever interpreter path one selects while setting up the environment.
You can modify the shell in tasks.json in a way that works for bash and cmd shells. This worked for me and it correctly prints the full path to the python executable in the virutal environment.
{
"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": []
}
]
}
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"
}
}
]
}
For people who maybe using Poetry a solution is to run
poetry shell
before running VSCode but a better solution is to do the following. This has been mentioned in an earlier answer but not specifically for poetry.
Use Poetry for Python Dependency
I fail when I tried to use IPython with SublimeREPL.
My setup is:
- Windows 8.1
- Sublime Text 3
- C:\Python34
- C:\Python27
- C:\Anaconda3\Scripts -> Here it's where Ipython is
My PYTHONPATH and PATH are correct and I wrote in SublimeREPL settings - User:
{
"default_extend_env": {"PATH": "{PATH};C\\Anaconda3\\Scripts"}
}
It's solved. I make a file named Main.sublime-menu within the folder Sublime Text 3\Packages\User\SublimeREPL\config\Python
[
{
"id": "tools",
"children":
[{
"caption": "SublimeREPL",
"mnemonic": "r",
"id": "SublimeREPL",
"children":
[
{
"caption": "Python",
"id": "Python",
"children":[
{
"command": "repl_open",
"caption": "IPython - Anaconda",
"id": "repl_python_ipython",
"mnemonic": "p",
"args": {
"type": "subprocess",
"encoding": "utf8",
"autocomplete_server": true,
"cmd": ["C:/Anaconda3/Scripts/ipython", "--colors=NoColor"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python"
}
}
]
}
]
}]
}
]
SublimeREPL doesn't run ipython executable directly - it uses its API to start the console, therefore IPython needs to be importable, what means it needs to be on PYTHONPATH. So, extend default_extend_env config with PYTHONPATH pointing it to IPython source folder.
There are 2 ways to make it, win10, conda python3.6.7, Ipython7.1.1, Sublime 3.1.1
1.Open \Packages\SublimeREPL\config\Python\Main.sublime-menu, find the line
"windows": ["python", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"]
and change it to where your ipython.exe located, for me is
"windows": ["C:\\Python\\envs\\py37\\Scripts\\ipython.exe"]
2.Just modified \Packages\SublimeREPL\config\Python\ipy_repl.py to 3 lines,
import os
# change dir to path_to_ipython.exe
os.chdir(r'C:\Python\envs\py37\Scripts')
os.system('ipython')
I prefer the second one, if you encount problems with first way, such as your shortcut about sublimeREPL not work (happens to me:(), try second one.