When executing "Run Selection/Line in Python Terminal"
command in VSCode, terminal's current working directory is the workspace root directory. How can we set current directory of terminal to the current file's directory when running the selection/line?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
you need to go to
file/preferences/user settings
and click the "{}" icon at the top right of the window. After that, put this setting in:"terminal.integrated.cwd": "C:\\Users\\myUser\\",
and after that wherever your terminal's directory happens to be. This answer is not the most inaccurate cause im still a noob myself at using vscode so if someone more experienced with it could reply to this thread it would be great.There is no straightforward way to achieve this yet. In search for a better solution, I have a workaround with the Terminal Here extension in the VScode Marketplace. This extension allows you to open an integrated terminal in the current file's directory. This extension combined with a few more steps and you should get the desired behavior.
ctrl+shift+p
and executeTerminal Here: Create Terminal
. This will create a new terminal in the file's directory.python
in the terminal to launch the Python interpreter.ctrl+shift+p
and executeTerminal: Run selected text in active terminal
. This will run that line of code in the open python interpreter.The first two steps are required only for the first time you run a code selection in the Python interpreter in the current file's directory. All subsequent selections can be run with the third step. To make things quicker, you could attach custom keybindings to the first and last steps.
Update following release 2019.10.44104
Following release 2019.10.44104 of the VS Code python extension, you can now set the
python.dataScience.notebookFileRoot
to${fileDirname}
to directly start the python interactive window in the directory of the file you're running.Note that the root directory will not change if you then run code from another file unless you interrupt/restart the kernel (or close VS Code). On this aspect, see the following comment and the corresponding github issue.
For the Python Interactive Window, the setting you're looking for is
python.dataScience.notebookFileRoot
. However, as explained in this answer to a similar question,See also the associated github issue.
As indicated, you can still set this setting to a specific absolute path, which might be enough if you're mainly working on a single project at a time.
Alternatively, you could also add the following code at the top of your script/notebook:
This options will help you. File->Preferences->Settings. Add or edit the below setting.
terminal.integrated.shell.windows": ""
From the next terminal it will be reflected.
And add .profile to your default shell and add default path to it.
More information at: https://code.visualstudio.com/docs/editor/integrated-terminal
In "User Settings", use the search bar to look for "python.terminal.executeInFileDir" and set (=) its value to "true" instead of "false".