How to run python interactive in current file'

2020-05-25 06:40发布

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?

5条回答
【Aperson】
2楼-- · 2020-05-25 06:54

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.

查看更多
Deceive 欺骗
3楼-- · 2020-05-25 06:57

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.

  • Once the extension is installed, make sure your file window is in focus, and press ctrl+shift+p and execute Terminal Here: Create Terminal. This will create a new terminal in the file's directory.
  • Type python in the terminal to launch the Python interpreter.
  • Now, position the cursor on the line you wish to execute and press ctrl+shift+p and execute Terminal: 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.

查看更多
祖国的老花朵
4楼-- · 2020-05-25 07:05

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,

Always opening on the file location (without having to set notebookFileRoot to an absolute path per folder) is not supported via the notebookFileRoot setting. The VSCode variables such as ${fileDirname} are specific to task and debug configuration files (launch.json and task.json).

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:

import os
os.chdir('absolute-path-to-workingDir')
查看更多
再贱就再见
5楼-- · 2020-05-25 07:08

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

查看更多
趁早两清
6楼-- · 2020-05-25 07:11

In "User Settings", use the search bar to look for "python.terminal.executeInFileDir" and set (=) its value to "true" instead of "false".

查看更多
登录 后发表回答