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.
Related
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
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.
I'm trying to do a basic thing: debug a python function app using a virtual environment.
The Visual Studio code documentation does not indicate how to do and I cannot figure it out myself: https://code.visualstudio.com/docs/python/debugging
I run the app from the terminal with the following command:
.venv36\scripts\activate
func start
The app is run from my virtual environment and everything is OK.
To debug, the debugger uses the launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Python Functions",
"type": "python",
"request": "attach",
"port": 9091,
"windows": {
"pythonPath": ".venv36\\Scripts\\python.exe"
},
"preLaunchTask": "func: host start"
}
]
}
Reading the https://code.visualstudio.com/docs/python/environments, I added the following settings to the settings.json:
{
...
"python.pythonPath": ".venv36\\Scripts\\python.exe",
"python.pipenvPath": ".venv36\\Scripts\\pip.exe",
"python.venvPath": ".venv36",
"python.terminal.activateEnvironment": true,
The tasks.json is:
{
"version": "2.0.0",
"tasks": [
{
"type": "func",
"command": "host start",
"problemMatcher": "$func-watch",
"isBackground": true,
"options": {
"cwd": "${workspaceFolder}/AlarmScoringFuncApp"
}
}
]
}
When I start debugging, the app is started but not in the right environment: it is using the system libraries instead of the ones installed for my project in the requirements.txt.
My app crash when it tries to use a library that is not installed in the system environment.
I'm new at django development and come from desktop/mobile app development with Xcode and related IDE.
I have to use Django and I was wondering if there was an efficient way to debug it using Visual Studio Code (or Atom).
Any help related to Django IDE would be helpful too.
For VSCode (full disclosure, I'm one of the VSCode developers) try installing the Python extension to get started.
This documentation covers debugging Django. There should be a included debug configuration or you can add your own to the launch.json file:
{
"name": "Django",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config.python.pythonPath}",
"program": "${workspaceRoot}/manage.py",
"args": [
"runserver",
"--no-color",
"--noreload"
],
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput",
"DjangoDebugging"
]
}
The Python extension also provide many other features that you may find useful.
VSCode has an official tutorial explaining this:
https://code.visualstudio.com/docs/python/tutorial-django
There are several steps that need to be taken, which I don't all want to write out manually, since there are quite some steps, but I'll try to summarize what needs to be done:
The text below is basically a partial copy of the above tutorial, I am not claiming I came up with this myself.
1. Make sure to check out the prerequisites (use VS Code Python extension, install Python on local machine) link to docs
2. Use Python virtual environment link to docs
Besides using a Python virtual environment, you also need to select the Python executable inside this virtual environment as the interpreter in VS Code. This can be done like so:
In VS Code, open the Command Palette (View > Command Palette or (Ctrl+Shift+P)). Then select the Python: Select Interpreter
Then you select the Python executable inside your virtual environment, which you can recognize by it's path.
3. Create debugger lauch profile
as described here, in the documentation
upper left of the VS Code window)
4. Now you can start debugging
this part of the documentation will give you an introduction on how to do that
Only experimental configuration works for me.
{
"name": "Django",
"type": "pythonExperimental",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true
},
Standard config causes Unverified breakpoint issue.
{
// 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: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\manage.py",
"args": [
"runserver"
],
"django": true
},
{
"name": "Django: makemigrations",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\manage.py",
"args": [
"makemigrations"
],
"django": true
},
{
"name": "Django: migrate",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\manage.py",
"args": [
"migrate"
],
"django": true
},
]
}
Nothing worked for me until I had disabled auto reload (--noreload as an argument is crucial, not really sure why it causes problem with debugging)
How do I run a Python program under debug and set the working directory for the run?
#SpeedCoder5's comment deserves to be an answer.
In launch.json, specify a dynamic working directory (i.e. the directory where the currently-open Python file is located) using:
"cwd": "${fileDirname}"
This takes advantage of the "variables reference" feature in VS Code, and the predefined variable fileDirname.
If you're using the Python: Current File (Integrated Terminal) option when you run Python, your launch.json file might look like mine, below (more info on launch.json files here).
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}"
},
//... other settings, but I modified the "Current File" setting above ...
}
Remember the launch.json file controls the run/debug settings of your Visual Studio code project; my launch.json file was auto-generated by VS Code, in the directory of my current "Open Project". I just edited the file manually to add "cwd": "${fileDirname}" as shown above.
Remember the launch.json file may be specific to your project, or specific to your directory, so confirm you're editing the correct launch.json (see comment)
If you don't have a launch.json file, try this:
To create a launch.json file, open your project folder in VS Code (File > Open Folder) and then select the Configure gear icon on the Debug view top bar.
Per #kbro's comment, you might be prompted to create a launch.json file by clicking the Debug button itself:
When I clicked on the Debug button on my navigation panel it said "To customise Run and Debug create a launch.json file." Clicking on "create..." opened a dialog asking what language I was debugging. In my case I selected Python
Configure the cwd setting in launch.json as follows:
{
"name": "Python",
"type": "python",
"pythonPath": "python",
...
"cwd": "<Path to the directory>"
...
}
This setting helps me: (I am a Windows person)
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"cwd": "${workspaceFolder}\\app\\js", // set directory here
"program": "${workspaceFolder}\\app\\js\\server.js", // set start js here
}
In some cases, it might be also useful to set the PYTHONPATH along with the workspaceFolder:
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"env": {
"PYTHONPATH": "${cwd}"
}
}
I am posting this sample configuration for people who use TypeScript on Node.js
in my project my Node.js server TypeScript files are located in folder Application_ts
and the compiled js files are generated in the folder named Application
because when we run our application in debug mode or start it normally we should start from Application folder which contains the js files
so bellow configuration run debug from root folder where my application_ts also exists and works perfect
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug TypeScript in Node.js",
"program": "${workspaceRoot}\\Application\\app.js",
"cwd": "${workspaceRoot}\\Application",
"protocol": "inspector",
"outFiles": [],
"sourceMaps": true
},
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"port": 5858,
"outFiles": [],
"sourceMaps": true
}
]
}
You can set up current working directory for debugged program using cwd argument in launch.json
To set current working directory to whatever file you are executing at the time:
File > Preferences > Settings > Python > Data Science > Execute in File Dir
Thanks brch: Python in VSCode: Set working directory to python file's path everytime
I faced the same issue and noticed that when running the which python command in Terminal in Mac it shows me a different path to what I get when I run the which python command in vs code. And also that my file runs properly in the terminal when run using python filename.py
So I copied that path from the terminal and pasted it in VS code into Preferences->Settings->Extensions->Python->Default Interpreter Path and it worked. I hope this helps.
I use the "justMyCode = false" so I can also debug and jump into the functions that the main script calls.
{
// 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": false,
"cwd": "${fileDirname}" }
]
}