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.
Related
I'm trying to get the below to run a docker container command via VScode.
Within tasks.json I have:
{
"label": "docker-run: debug",
"type": "docker-run",
"dependsOn": [
"docker-build"
],
"python": {
"module": "simply.py"
},
"dockerRun": {
"command": "python3 simply.py",
}
},
Which I would expect to be run as
docker container run [unrelated arguments] python3 simply.py
Where python3 simply.py is the command I want to run
Instead I am getting
docker container run [unrelated arguments] python3` simply.py
I'm not sure where VScode is generating this tick after python3 but the result is my code doesn't run.
An example of dockerRun with python3 within command might help indicate what I'm doing wrong.
Also running docker container run [unrelated arguments] python3 simply.py in the terminal works fine.
Thanks
extra:
Not sure if relevant but my launch.json is:
{
"name": "Docker: DEV",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
},
},
This fixed itself upon closing and opening VScode
I have my Flask installed in my main directory for playing with Flask projects and I don't want to change it (its name is tryFlask).
My project is in one of the subdirectories to tryFlask (main_project).
How should I config my launch.json so that it doesn't throw errors when I try to debug?
Current config which throws:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "my_app.py",
"FLASK_ENV": "tryFlask",
"FLASK_DEBUG": "1"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"jinja": true,
"justMyCode": true
}
]
}
And yes I have already checked "FLASK_APP": "my_project/my_app.py". Doesn't work. Neither does reinstalling dependencies or even choosing different interpreters from venv's bin subdirectory.
If you want to use flask with vscode, following this tutorial is a good start.
According to the question in your post, if you want to specify the python interpreter when debugging, you can add the python configuration to launch.json
Simple example:
{
// 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,
// Modify the python interpreter with flask installed on your machine
"python": "C:\\WorkSpace\\pytest10\\.venv\\Scripts\\python.exe"
// "python": "${workspaceFolder}/.venv/Scripts/python.exe"
}
]
}
For more launch.json configuration, you can check this link.
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 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.
I currently have a flask application in which the file structure looks like this:
C:\Users\kmelton\Python\Flask\BGSCS-API-dev\InterjectApi\server.py
And the folder I currently have open as a workspace in VS Code is the BGSCS-API-dev folder.
My launch.json currently looks like this:
{
// 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: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "server.py",
"FLASK_ENV": "development"
},
"args": [
"run",
"--no-debugger"
],
"jinja": true
}
]
}
And upon every debugging attempt, I get an error.
* Serving Flask app 'server.py' (lazy loading)
* Environment: development
* Debug mode: on
Usage: python -m flask run [OPTIONS]
Try 'python -m flask run --help' for help.
Error: Could not import 'server'.
I know that there is probably something I need to add to my launch.json file to path to the file properly, but the things I have tried haven't worked for me. Thanks
Okay, so I've solved the initial error I was having, only to be faced with a new error pertaining to another import issue, this time actually inside the code, importing a file that imports fine when running the program normally.
The fix I implemented to my code is as follows (within 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: Flask",
"type": "python",
"request": "launch",
"module": "flask",
# Added new line cwd to specify initial working directory
"cwd":"${workspaceRoot}",
"env": {
# Added ${workspaceRoot}/InterjectApi/"file name trying to run"}
"FLASK_APP": "${workspaceRoot}/InterjectApi/server.py",
"FLASK_ENV": "development"
},
"args": [
"run",
"--no-debugger"
],
"jinja": true
}
]
}