3

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:

error

"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:

desired

Please help! Thanks

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

is there a line / property in the settings.json I can modify to do this?
and thanks for the suggestion, but I prefer regular python shell over ipython (if that's what you mean)
as a work around.... I have this set - "python.terminal.executeInFileDir": true .. I can do a quick ctrl+A and comment the entire script and run the file. This brings me to the file directory, then I can just uncomment the code and start runnings line by line from the file directory
I actually don't mean IPython. We have a custom Interactive Window experience that you learn about at code.visualstudio.com/docs/python/jupyter-support-py.
sorry yes, this is what I was referring to. Do you know if its possible to switch the Enter / Shift+Enter shortcuts for python interactive? really the only reason I don't use it... see stackoverflow.com/questions/59809829/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.