简体   繁体   中英

Run active python file in active terminal or specific location pointing terminal with keybinding

In VsCode I am trying to find how i can:

Run ACTIVE python file in ACTIVE integrated terminal with shift+enter

Note: I already have a key binding to "Run active file in active terminal" but i does not include the python interpreter path/keyword before launching the command so it is just opening the file)

I know there are multiple ways to run python file in terminal (F5 for debug etc...) and "Shift+Enter" works quite well if you do not have to deal with location of the running file.

In this case when "Shift+Enter", I would like to "cd my/project/folder/where/my/file/is" and then "python3 myFileToRun.py", so having a terminal opened at the file's location will allow me to quickly hit the shortcut without any locations issues.

I see 3 steps to respect:

  • open terminal -> (?cmd to launch terminal in vscode and place in editor mode)
  • cd to file's folder location -> (cd C:/complete/path/to/file)
  • run python file (python3 myPythonFile.py)

Here is an example of what i have tried:

keybindings.json

    {
        "key": "shift+enter",
        "command": "-python.execSelectionInTerminal",
        "when": "editorTextFocus && terminalFindFocused && terminalProcessSupported && editorLangId == 'python'"
    },

Python: Run Python File command calls python with absolute paths for both an interpreter and script. It does not perform cd though, instead running from the project's default location.

It's the same as the Run icon visible when you have.py file opened with Python extension installed. You can reassign default Shift-Enter command (Python: Run Selection/Line in Python Terminal) or you can create a new binding, eg Ctrl+Enter and copy When from the Shift-Enter one, whichever suits you more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM