Reputation: 4638
Recently made the move from Spyder to VSCode. Though I quite enjoy VSCode, it is a tad steep for someone new, especially coming from an IDE like Spyder that is basically out of the box ready to go (albeit less customization).
I'm having a particular issue that I can't seem to figure out.
Frequently, I like to step through my code line-by-line as I'm writing it. This means when I run a line of code, the terminal launches the python shell and runs it (if it's the first command I run, otherwise it will just run in the open shell).
My keyboard shortcut to run selection/line is F1.
When I press F1, the terminal opens and launches the python shell and runs the line. My issue is that I want it to cd into the directory where the file resides before running the command.
I was able to get this working for debug mode and when running the entire file:
debug launch.json:
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}"
}
settings.json:
"python.terminal.executeInFileDir": true
How can I get the python shell to open in the file directory automatically?
I tried doing:
"python.terminal.launchArgs": ["cd ${fileDirname}"]
But it appends it after the python launch command:
"python.terminal.launchArgs": ["-c import os; os.chdir(${fileDirname})"]
"terminal.integrated.cwd": "${fileDirname}"
"python.testing.cwd": "${fileDirname}"
these did not work either..
How can I make VSCode launch the python shell from the current file directory when running current line/selection?
I would like it to work like this:
Please help! Thanks
Upvotes: 3
Views: 3211
Reputation: 16100
You would need to open the directory containing your code as the workspace to get what you're after so the shell has the appropriate current working directory. Otherwise you can change the directory programmatically via os.chdir()
.
You can also use the Interactive Window to get a similar experience/result if you would rather not have this all happening via the terminal.
Upvotes: 1