I started to use visual studio code yesterday and i'm having this problem...
Let's suppose I have this code:
myFile = open("file.txt", "w+")
myFile.write("something \n")
myFile.close()
When I run it on VScode it don't save the file, but when I run it in SublimeText it does.
do you know how to solve it?. I didn't find anything (or I don't know how to search), my launch.json look like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "internalConsole"
}
]
}
**After I wrote that I noticed that it saves the file in "C: \ Users \ User \ Documents \ vsprofiles", but I have my .py file on the desktop, I want it to save the file in the same path where the .py file is, like SublimeText does.
For completeness here is my config file
"launch": {
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}",
"env": {},
"pythonArgs": [],
"stopOnEntry": false
}
]
}
Related
My launch.json config looks as follows:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"python": "/Users/username/miniconda3/envs/env_name/bin/python",
"program": "${file}",
"console": "integratedTerminal",
}
]
}
However, when I run the debugger, the specified env_name is not run. Instead the default interpreter python version (3.10) is run. Why is the specified environment not being activated?
I am trying to find a way to give current file as argument for module. I couldn't find it anywhere so I'm trying stackoverflow
{
"name": "Python: Synapse",
"type": "python",
"request": "launch",
"module": "projFolder.targetModule",
"console": "integratedTerminal"
}
Manually specifying the module argument such as above example, works. But thinking of doing that for every each file wanted me to automate it. I've tried ${file}, but it gives the file path not the module. So it didn't work. How can I launch current current file as module?
ps: answer from https://stackoverflow.com/a/57416114/6088796, but I modify a litter
add something like this to your launch.json, but if you want to run multi-module(in a different place, the only way you can do is using multi configuration, or use a plugin(see below)
"configurations": [
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "yourmodule.${fileBasenameNoExtension}",
"stopOnEntry": true
},
You can use the extension Command Variable to get this relative directory with dot separator.
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Module CmdVar",
"type": "python",
"request": "launch",
"console": "integratedTerminal",
"module": "${command:extension.commandvariable.file.relativeDirDots}.${fileBasenameNoExtension}",
}
]
}
I have installed Visual Studio Code with the Python extension in Linux Manjaro. When I try to launch a Python script, the external terminal opens but after 5s I get an error message in a window telling me "timeout" and my script doesn't launch.
I've seen this post with the same problem on Windows 10 but the fix doesn't seem to work on Manjaro. Any idea?
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 (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
]
},
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "enter-your-module-name-here",
"console": "integratedTerminal"
},
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/IA_TD2.py",
"console": "integratedTerminal",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true
},
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "app.py"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"jinja": true
},
{
"name": "Python: Current File (External Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal"
},
{
"name": "Python: Current File (None)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "none"
}
]
}
Unfortunately VSCode is not compatible with every terminal and it seems not (yet) compatible with your Linux Manjaro installation with KDE Desktop Environment.
Switching to another terminal will surely solve this (i.e. GNOME Terminal). I'm not sure, if the gnome-terminal package is aviable without installing the GNOME Desktop Environment.
Here is a good solution how you can do this without reinstalling the whole OS.
I have a project and am trying to debug my main.py. I am really confused why I am getting the following error from the imports at the top of my file (only) when running the debugger:
Exception has occurred: ModuleNotFoundError
No module named 'bbb'
File "/Users/maxepstein/myproject/bbb/train/__main__.py", line 8, in <module>
from bbb.mysubfolder.myfile import myfunction
My project folder structure, as shown by these print statements (as shown by the debugger) confirms my 'bbb' module exists, and has an __init__.py:
import os
print(os.getcwd())
print(os.listdir())
print(os.listdir('bbb'))
/Users/maxepstein/myproject
['requirements.txt', 'bbb', 'resources', '__init__.py', 'readme.md', 'results', '.gitignore', '.git', '.vscode', 'bbenv']
['config', 'tests', '__init__.py', 'utils', 'predict', 'train']
I'm trying to debug as "debug current file - integrated terminal", below is the applicable debug settings from my debug settings.json. After searching online, I really thought adding "cwd": "/Users/maxepstein/myproject" below would be my solution but it hasn't helped.
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "/Users/maxepstein/myproject"
}
A simple workaround to the bug mentioned by #BrettCannon is to add the following env entry to the launch.json configuration:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"env": { "PYTHONPATH": "${workspaceRoot}"}
}
]
}
Had the same problem when importing from a nested directory, and fixed it by appending to the env variable PYTHONPATH:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"env": {
"PYTHONPATH":"${PYTHONPATH}:/home/maxepstein/myproject/"
}
}
]
}
When I am debugging a Python module in VS Code I use the Module debug configuration instead of the Current File one. For you it might look like this:
{
"name" : "Python: Module",
"type" : "python",
"request": "launch",
"module": "bbb",
"args": []
}
See the documentation https://code.visualstudio.com/docs/python/debugging
Also, in VS Code, these steps will auto-populate these settings for you:
Debug -> Add Configuration -> Python: Module
In my case, I quickly fixed it selecting the right interpreter:
You can use the current file debug configuration. In the file you're debugging that's importing the modules add the full path to the modules you're trying to import to your system path.
sys.path.append('/Users/my_repos/hw/assignment')
import src.network as network
The module here is src, located in the assignment directory.
I run the debugger from VS Code.
My structure in VS code:
myproject
+vscode
+---launch.json
|
+src
+---test/
+------MainTest.py
+---Main.py
the launch.json that saved me:
{
// 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}",
"env": {"PYTHONPATH": "${workspaceRoot}:src"},
"console": "integratedTerminal"
}
]
}
python3 supports -X dev startup mode.
This feature is becoming very useful for code "environment awareness" and for execution functionality.
For ex. the debug prints of asyncio module https://docs.python.org/3/library/asyncio-dev.html#debug-mode-of-asyncio
How can I configure vscode to start python with this flag ?
vscode now supports pythonArgs in launch.json, but note that it expects a format of ['-X', 'dev']:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"pythonArgs": ["-X", "dev"],
},
]
}