python vscode environment variables not available - python

I have attempted to follow the directions for environment variables in my project and thought I was doing the correct thing but it does not appear to be working when debugging my python code in vscode.
I have a launch.json for a project in .vscode/
{
// 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,
"envFile": "${workspaceFolder}/dev.env"
}
]
}
and a dev.env file in my project root
PYTHONUNBUFFERED=1
PASSWORD=abc123
DEBUG_SSO=user.name
CELERY_DEBUG=True
SEND_MAIL=True
when I hit a breakpoint and do "os.getenv('PASSWORD')" it returns None, even though the variable is set in dev.env.
Any idea what is wrong with this configuration?

This may be due to the relative path being unrecognized.
Add this line to the file "launch.json" : "cwd": "${fileDirname}"

Related

'No module named flask' in VSCode even when I have installed flask

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.

How to debug a file from current directory with Python in VS Code?

I can see that this question has been asked multiple times already however, none of the solutions seems to be working anymore, forcing me to ask for the latest fix.
Problem
I have created a workspace in VS code with the following directory structure,
work__assignments (this is my main workspace folder in vs code)
assignment__1
data
modules
tests
assignment__2
data
modules
tests
"work__assignments" is my main workspace folder in the VS code. From this main workspace, I go to an assignment folder (e.g. work__assignments > assignment__2 > modules) and work on the respective code. However, when I try to debug the code in "work__assignments > assignment__2 > modules" the debugger loads from the main workspace folder (i.e. work__assignments), then it fails because it can't find other modules in the modules folder "work__assignments > assignment__2 > modules".
I have tried the following methods so far,
Updating the launch.json file and adding the line "cwd": "${fileDirname}",
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}"
}
Updating the launch.json file and adding the line "cwd": "",
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": ""
}
Updating the launch.json file and adding the following lines,
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"env": {
"PYTHONPATH": "${cwd}"
}
}
Updating the preferences
However, NONE OF THESE APPROACHES HAVE WORKED. Every time I debug the code, it launches into the workspace folder and not in the code folder. What am I doing wrong?
Note: I am running the debugger using the "Debug Python File" button at the top right corner as shown in the picture below,
Configure one of the following cwd in launch.json:
"cwd": "./assignment__2"
"cwd": "${fileDirname}"
It should be noted that you need to debug the program from the Run and Debug panel to execute the configuration in launch.json.
A simple example
Directory Structure and code
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
// "cwd": "./hello"
"cwd": "${fileDirname}"
}
]
}
Debug results
Invalid configuration in launch.json if debug is selected from play button

Time out waiting for launcher to connect in VS code

I did python debugging in VS code.
The following is the 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",
"stopOnEntry": false,
"python": "${command:python.interpreterPath}",
"program": "${file}",
"cwd": "${workspaceFolder}",
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions":[
"RedirectOutput"
],
"console": "integratedTerminal"
}
]
}
The following is settings.json file:
{
"python.pythonPath": "c:\\Users\\susan\\Documents\\PythonScripts\\venv\\Scripts\\python.exe",
// to fix 'Timeout waiting for debugger connections'
"python.terminal.activateEnvironment" : false
}
When I debug the python script in VS code, I got Time out waiting for launcher to connect and cannot debug the python script.
May I know how can I solve this issue?
Its very simple. Open the launch.json file and add the following into it:
{
"name": "Python: Debug Console",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "internalConsole"
}
Then save and exit it. Whatever you do, DO NOT clear the text already in there or else it may make it worser
If for whatever reason "internalConsole" isn't a good solution:
in your shell script:
export PROCESS_SPAWN_TIMEOUT=30
or just hack the code directly (will be reverted if you update the extension):
.vscode-server/extensions/ms-python.python-2022.18.2/pythonFiles/lib/python/debugpy/adapter/launchers.py[161]:
change:
timeout=(None if sudo else common.PROCESS_SPAWN_TIMEOUT)
to:
timeout=30,

VS Code: ModuleNotFoundError when using debug

I develop a Django project
I ma newbie in Django and VS Code so I "debug" using print() and console.log() which is not efficient...
so I really need to be able to debug my code and follow variable value during execution but when I try to run debug
I read the VS Code doc about django
so I have set the launch.json like that:
{
// 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 : Fichier actuel",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"console": "integratedTerminal",
"args": [
"runserver",
"--noreload"
],
"django": true
},
]
}
I select Python: Django in the debug configuration list and run my project and have ModuleNotFoundError: No module named 'simple_history' and other...
I don't understand because my project works so tierce app are correctly installed in my env
It wonder if it could be link to another problem I have since I start using python/VS Code and was not able to fix (I already post on this problem: python can't load on VS Code):
The environment variable 'Path' seems to have some paths containing the '"' character. The existence of such a character is known to have caused the Python extension to not load. If the extension fails to load please modify your paths to remove this '"' character.
Someone can help me making debug operational?
thanks !

VSCode -- how to set working directory for debugging a Python program

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}" }
]
}

Categories