Visual Studio code and virtualenv - python

I am trying to use Visual Studio Code with virtual environment. In the Launch JSON I specify the nosetests launch like this:
{
"name": "nosetests",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"program": "${workspaceRoot}/env/dev/bin/nosetests",
"args": [
"--nocapture",
"tests"
],
"externalConsole": false,
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit"
]
},
However when I launch the environment variables do not get picked up. I've tried setting up the python path in workspace settings:
"python.pythonPath": "${workspaceRoot}/env/dev/bin/python"
but it doesn't seem to set the right environment. There needs to be something that' the equivalent of source activate. Has anyone figured it out?

#mikebz you need to configure the path to the python executable as follows:
"pythonPath":"${workspaceRoot}/env/dev/bin/python"
The path may not be 100% accurate (please double check it), but that's how you need to configure it in launch.json.
With the next version of VS Code you will no longer have to do this, i.e. you won't have to configure this same setting in two files.
More details on configuring the path for debugging can be found here:
https://github.com/DonJayamanne/pythonVSCode/wiki/Python-Path-and-Version#python-version-used-for-debugging

As 2018.03 there's an article how to add virtualenv to your python path list per particular workspace: https://code.visualstudio.com/docs/python/environments
All you have to do is File->Save workspace as.. and then add to the settings of workspace virtualenv:
{
"folders": [
{
"path": "."
}
],
"settings": {
"python.pythonPath": "${workspaceFolder}/.venv/bin/python"
}
}

You need to set up the path for your virtual environments in your workspace settings file: https://code.visualstudio.com/docs/python/environments
Save your workspace then open <workspace_name>.code-workspace file and add your virtual environment folder to the settings:
{
"folders": [
{
"path": "<path-to-your-workspace>"
}
],
"settings": {
"python.venvPath": "<path-to-your-virtual-env-folder>"
}
}
After that open command palette (CMD/CTRL + SHIFT + P) and type select interpreter. Then choose the one from the desired virtual environment.

Related

VSCode Python debugger no longer stopping at breakpoints after moving project directory

I just moved my project to a new directory on my computer. The debug config executes but no longer stops at breakpoints.
When I move the project folder back to its original location, it's working again.
Thanks for your help!
My launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": "Module Python",
"type": "python",
"request": "launch",
"cwd": "${workspaceFolder}",
"module": "bin.main",
"justMyCode": true
}
]
}
Config :
MacOS 10.15
VSCode 1.69.2
Conda virtual env with python 3.7.13
python extension ms-python.python-2022.13.12381007

Exception has occurred: ImportError Vscode While Debugging

I am trying to try the debug tool in my vs code I am totally new to vs code and I am getting an error such as this when I run it
ERROR MESSAGE I have a folder called .vscode with a launch.json file `
"version": "0.2.0",
"configurations": [
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\myPro\\manage.py",
"args": [
"runserver"
],
"django": true,
"justMyCode": true
}
]
}
I watched a lot of videos on youtube and I see people who have settings.json in there .vscode file.
WHAT I HAVE DONE:
I have downloaded django == 3.25
I have downloaded python == 3.10.4
IN a virtual environment my environment works perfectly fine its just that I can find the reason to this problem I would really appreciate it if someone can help me out
Try Ctrl + Shift + P and the option "Select Python Interpreter", then select the one in which the environment is. This should have VSCode running in the same environment.
Please change your debug settings to your Virtual Environment
Open .py file and then debug it.
You may have to restart vscode

How to use Mamba Miniforge CLI in VSCode with Windows

I am trying to debug some Python code that must run in a Mamba environment. In order to run the code (but not debug), I can open the Miniforge Prompt command line application, activate my enviroment (mamba activate my_env), and then run my python file (python my_file.py). Running this code is producing an error that I would like to trace back using the Visual Studio Code debugging interface. I am having a problem trying to get this to run in Visual Studio Code, because it cannot seem to run the Miniforge Prompt command line. I am also running on Windows 10.
The default terminal options (for Windows) in VSCode are Powershell and CMD (and Git Bash), which both work fine, however, when I added another terminal method for Miniforge (via the settings.json), it doesn't seem to be working properly.
Here is my settings.json file:
{
...,
"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"
},
"MambaPython": {
"path": [
"${env:windir}\\System32\\cmd.exe"
],
"args": ["\"/K\"", "C:\\ProgramData\\mambaforge\\Scripts\\activate.bat", "C:\\ProgramData\\mambaforge"],
"icon": "terminal-cmd"
}
},
"terminal.integrated.defaultProfile.windows": "MambaPython",
}
I also modified the launch.json to activate the mamba environment once running in the miniforge CLI. Here is my launch.json file:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
},
{
"name": "Python: ProjectEnv",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"preLaunchTask": "ProjectEnv_activate",
"args": ["--kwarg_one=Something", "--kwarg_two"],
}
]
}
also, here is the tasks.json file that actually activates the environment:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [{
"label": "ProjectEnv_activate",
"command": "mamba activate ProjectEnv",
"type": "shell"
}]
}
When I execute any code (in run or in debug) in VSCode, it appears to just run with the standard CMD terminal, not in a Mamba environment as specified. If anyone knows how to get this to work, or any way to activate a Mamba environment when debugging python in VSCode, any help would be much appreciated!
I came across this question trying to configure my VS Code terminal to play nice with miniforge. I got it working correctly after just fiddling with the args syntax a bit. I'm using conda, not mamba, but I don't think that should matter for activating the environment.
"Command Prompt": {
"path": [
"${env:windir}\\System32\\cmd.exe"
],
"args": ["/K", "C:\\Users\\johndoe\\AppData\\Local\\miniforge3\\Scripts\\activate.bat","C:\\Users\\johndoe\\AppData\\Local\\miniforge3\\envs\\base" ],
"icon": "terminal-cmd"
},
There is a vscode extension
https://github.com/mamba-org/vscode-micromamba
With this extension one can create mamba environments based on environment.yml file.
Next to mamba environment a .env file is generated. If you use python vscode extension you could configure it to use the .env file. When using terminal window the environment is activated automatically.
I had no luck with the micromamba extension.
I'm using micromamba, but the workaround should be the same.
Try set this alias in your powershell $profile:
Set-Alias conda mamba
Now the env is also activated, when the terminal starts in vs code.

How to run python module command from VSCode tasks

I am trying to run my alembic migration which is in a subdirectory so I am writing VSCode tasks for it but when I run it I get command not found error:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Alembic autogenerated revision",
"type": "shell",
"command": "alembic revision --autogenerate -m init",
"options": {
"cwd": "${workspaceFolder}/my_sub_directory"
}
}
]
}
How can I run it under my virtual environment?
Activating the virtual environment, then installing alembic, you can run alembic revision directly, that's because it's in the virtual environment and able to call the module.
So, when executing alembic command in task, you also need to activate env first.
Copy the activation command and paste it before alembic and separated by ; , the task should be ran successfully:
Unfortunately, I've not been able to find a solution that doesn't require hardcoding the virtualenv path.
I believe an extension would be able to solve this, possibly by adding support for python in the tasks's type field.
A workaround
Use launch configurations instead of tasks for running python scripts. It is a bit weird, but it works.
Here's your code adapted to launch.json schema:
{
"version": "0.2.0",
"configurations": [
{
"name": "Alembic autogenerated revision",
"type": "python",
"request": "launch",
"module": "alembic",
"args": ["revision", "--autogenerate", "-m", "init"],
"cwd": "${workspaceFolder}/my_sub_directory"
}
]
}
Also, consider using input variables for specifying revision messages and other variable things.
Thanks for your responses. To resolve this we can use the selected interpreter path and run the module.
{
"label": "Alembic autogenerated revision",
"type": "shell",
"command": "${command:python.interpreterPath} -m alembic revision --autogenerate -m init",
"options": {
"cwd": "${workspaceFolder}/ts_tools"
}
},
Reference: https://github.com/microsoft/vscode-python/issues/18728
Regards.

how to add optirun to vscode debug options?

I use manjaro and want to use vscode tool for debug my python program,but i use CUDA, so i usually use a command like this:
optirun ipython program.py arg1 arg2
when i try to debug the program optirun isn't called, so i don't have access to GPU, how can i call optirun before python (or ipython)?
I've tried to change settins.json and launch.json to add it to command but it doesn't work.
Thanks.
This is what I did and it worked.
Make a bash script, name it anything, assume script.sh.
#!/bin/bash
optirun python "$#"
In .vscode/launch.json set "pythonPath" to ./script.sh.
Here is a example launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"linux": {
"pythonPath": "./script.sh"
},
}
]
}
Now it should work as intended.
I also faced the same problem while debugging a OpenCL application in python. My Nvidia graphics card was managed by optirun. $# passes all the command line arguments supplied by vscode to the python interpreter.
You could try editing your launch.json to point to optirun as your Python interpreter via the "python.pythonPath" setting.

Categories