My debugger doesn't even begin to run my code. I press F5, the debug tab opens, shows that it is loading, and after a while it says "Session-1 timed out waiting for debuggee to spawn" in a pop-up window. I'm using VS Code version 1.40.1, I have my virtual environment setup, and the debugger used to work, stopping at breakpoints and changing the color of the blue bar at the bottom of the screen. Issue appeared while messing with the open() function, but the debugger doesn't work with any file.
I have seen and tried the solutions offered here and here. I don't use Conda, Jupyter, or any extensions besides the standard Python extension.
Code:
import os
def fib(n):
if not os.path.exists("Fibfile.txt"):
with open("Fibfile.txt", "w") as file:
file.write("1\n2\n")
with open("Fibfile.txt", "r") as file:
contents = file.readlines()
data = []
for item in contents:
# removes newline
data.append(int(item[:-1]))
with open("Fibfile.txt", "a") as file:
if n <= len(data):
return
else:
while n > len(data):
data.append(data[-2]+data[-1])
file.write(f"{data[-1]}\n")
fib(100)
My 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: Arquivo Atual",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
My solution is downgrade Python extension for Visual Studio Code.
You can download from GitHub release.
PTVSD release 2019.10.44104 is fine with VS Code 1.40.2.
Unchecked Extensions: Auto Update/Auto Check Updates
and install from VSIX manually.
Update: Newer version VS Code 1.41 fix this issue already.
the python version is conflict with python debugger version.
Change an older python debugger version or python version.
Related
I installed sympy using the commands
source website-env/bin/activate
(website-env)pip install sympy
My Python script is
import sympy as sp
u1,u2,d1,d2=sp.symbols('u1,u2,d1,d2')
psip,psim,phip,phim=sp.symbols('psip,psim,phip,phim')
psi=u1*(phip-phim)*u2-u1*(psip-psim)*d2-d1*(psip+psim)*u2+d1*(phip+phim)*d2
sp.factor(psi,phip,phim,psip,psim)
It runs fine from the command line but when I select this Python interpreter and try to run it within VSCode, it give "module not found - sympy"
My launch.json is
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
What could be the problem?
It seems there was something wrong with my virtual environment. I used pip freeze to get a list of modules, then deleted the environment and recreated it. That fixed it.
I am building an application in Python. A while ago, I was able to debug it without any issues, but after accidently reseting my VS Code settings, I am not able anymore. Whenever I try to debug I get the following error on the first installed library I try to import:
Exception has occurred: ModuleNotFoundError No module named
'PySimpleGUI'
When I try to install the library using pip3, I get the message:
Requirement already satisfied: PySimpleGui in c:\users\adassa\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages
When I run directly from the console using python3 name_of_file.py, the file runs without any problems. Here is my launch.json for debugging:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"env": { "PYTHONPATH": "${workspaceRoot}"}
}
]
}
I read in other questions that this error has to do with different installations of Python, and the debugger trying to find the library on the wrong directory, but couldn't understand how to solve it.
I tried replacing PYTHONPATH with the path of python3 I get from the console. (I obtaineed that path using the command from this answer: https://stackoverflow.com/a/647798/14874778, but the error remains. How can I solve this problem?
Do you have multiple python installations? If so, try something that looks like this (made for python 3.7.9 & Windows):
py -3.7 -m pip install pysimplegui
This has always worked for me.
You can also try specifying which installation you want to run the script, which should look like this (also 3.7.9, also Windows):
py -3.7 helloworld.py
I am trying to debug my python program in VSCode where I'm getting its directory. When I run os.getcwd() from the terminal, I get the correct directory, but when I use the VS Code debug option, it defaults to the "default" path (as set in my registry variable, which is C:\Users<User>\Downloads).
I have created a launch.json file.
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "C:\\Users\\<User>\\Documents\\Project\\"
}
]
}
Here I added "cwd", except no matter what value I put here, the value of os.getcwd() returns the default path in debug mode. I have tried putting: the whole path, ${workspaceFolder}, ${fileDirname}, ${fileWorkspaceFolder}.
The launch.json file is in the .vscode folder in my project.
I do not understand why this is happening and would ideally like a fix. None of the other questions on this site on this subject were able to help.
For those that might have the same problem in the future, I found a work-around. (This works without launch.json.)
I manually edited the code from: directory = os.getcwd() to:
dir_path = os.path.dirname(os.path.realpath(__file__))
os.chdir(dir_path)
directory = os.getcwd()
Then the debugger is in the current working directory and is able to see the files I needed it to.
Please use settings similar to the following in "launch.json":
"cwd": "${workspaceFolder}\\a_pythonscript",
When my "main.py" is in the folder "demo_csv" and "lauch.json" uses "cwd": "${workspaceFolder}\\demo",:
Since the python debugging function in VS Code is provided by the Python extension, please try to reinstall the Python extension.
**********New post edition with a simple example********************
After the suggestion of Martineau, I tried a simplified example python program in the following.
The background was that last week Vscode asked me to update to VSCodeUserSetup-x64-1.31.1 in Win10.
So I did.
Then sometimes VSCode failed to execute my main module.
I tried to reinstall and uninstall vscode. I also tried to modify the path variables here and there.
But the problem is still there.
Still I can execute the program OK purely in the console.
The simplified python program is the following.
print ("Here I am!")
The launch.json file is the following.
{
"version": "0.2.0",
"configurations": [
{
"name": "vsCode Test ",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"program": "${workspaceRoot}/src/pt.py",
"args": [
],
"cwd": "${workspaceRoot}",
"env": {},
"envFile": "${workspaceRoot}/.env",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
]
}
]
}
After I clicked the debugger button and then the right triangle button to execute the debugger, I always got the following screenshot which showed that nothing was executed.
I suspect that the new update has changed my settings in vscode.
But I don't know where to reconfig vscode.
Your help will be deeply appreciated!
Thanks
Farn
****************Old post with a more complicate example!****************
dear all:
Last week Vscode asked me to update to VSCodeUserSetup-x64-1.31.1.
So I did.
Then sometimes VSCode stopped to execute my main module.
The basic screen layout is the following.
VSCode basically stalled before the first statement and the first breakpoint.
But if I use Console to execute "python CnTaaD.py -L", the module (CnTaaD.py) executed correctly.
I tried several times and even reinstall, uninstall, used an older version of VSCode.
The stalling problem came on and off.
Then last night, VSCode always stalled before the first statement and the first breakpoint.
My friend helped me and guessed it is because the path to python interpreter is wrong now.
But we tried to set the path variables here and there.
Nothing good happened.
The code of CnTaaD.py is in the following for your reference.
It calls another module.
Can anyone help by telling me how to reconfigure maybe VSCode so that I can resume the development ?
Thanks
from __future__ import print_function # (at top of module)
if __name__ == '__main__':
import os
import sys
import CnUserManager
if sys.version_info[0] < 3: # Python 2 and 3:
print ("python 2.x")
# to execute the file, type in the following command in powershell:
# % python CnTaaDPackage.py build_ext --inplace
import future # pip install future
import builtins # pip install future
import past # pip install future
import six # pip install six
else:
print ("python 3.x")
root = os.path.realpath(__file__)
# print ("realpath of __file__ is ", root)
root = os.path.dirname(root)
root = os.path.dirname(root)
sys.argv.append(root)
# print ("sys.argv = ", sys.argv)
CnUserManager.CnUserManagerClass(sys.argv)
I kind of solved the problem.
Basically, I removed the "RedirectOutput" option from the "debugOptions" in launch.json. Somehow, the older version the redirection is to the console in the debug screen.
But now I am not sure where it is to.
In fact, there are three consoles in VSCode, internal, integrated, and external.
I will appreciate it if someone can enlighten me which is which ?
I checked some related pages. But vague explanations are found.
I'm trying to run a Python script from Visual Studio code, but the script fails to run and crashes with a SyntaxError pointing to the comment at the beginning of launch.json.
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 | Default",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config:python.pythonPath}",
"program": "${file}",
"cwd": "${workspaceFolder}",
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions": [
"RedirectOutput"
]
}
]
}
Terminal Output:
File ".../.vscode/launch.json", line 2
// Use IntelliSense to learn about possible attributes.
^
SyntaxError: invalid syntax
settings.json:
{
"python.pythonPath": "${workspaceFolder}/venv/bin/python"
}
I was working on my Windows machine earlier and all of this worked perfectly fine. For some reason, VSCode is trying to run the launch.json file through Python and // is an invalid comment syntax in Python. If I remove the comments, I get this error:
Traceback (most recent call last):
File ".../.vscode/launch.json", line 8, in <module>
"stopOnEntry": false,
NameError: name 'false' is not defined
If I use Python's False, I don't crash but nothing happens and my script does not run. It seems very much like launch.json is being parsed by Python erroneously. Any fix for this?
I found my problem. I did not update the program key to always point to my main.py. Instead, the current open file was being executed as a Python script -- launch.json Changing the program key or navigating to a different file solved the problem. Obvious once you notice it!
Solution 1
I consider that an easier solution is:
Close the launch.json on the editor group
Open the python file such as main.py to be debugged
[Run]-[Start Debugging] (F5)
As Nick mentioned, when focusing on the launch.json in the editor, the debug system runs on the launch.json itself, not a python file.
Solution 2
Modify the "program" in the launch.json as below:
"program": "${workspaceFolder}/main.py",
It corresponds to
the program key to always point to main.py
as Nick said.
Note that the above modification may not work well if the main.py places in a deep directory.
Closing launch.json if it is open for editing may solve the issue
If launch.json is the latest open file, VSCode may be trying to run launch.json as a Python module (despite the fact that it's clearly not a Python module).
See the NameError in the OP's third screenshot - looks like Python interpreter running against launch.json
(Note: the contribution of this answer is solely to put the crux of Haru's Solution 1.1 and Nick's own self-diagnosis into simple language in the answer's first line)