VSCode Remote SSH: Run Python File in New Terminal - python

I am using the Remote SSH extension for VSCode. Whenever I try to run a Python file in the Python terminal using the Run Python File in Terminal command, while the terminal is already executing another python program, VSCode tries to execute the new program in the same Python terminal, instead of opening a new terminal. Is there a way to execute each python program in a new terminal so that multiple programs can run simultaneously?
Edit: Tried to open a new Python terminal explicitly, but still, the Run Python File in Terminal is always routed to the first terminal. If I run the Python program explicitly in the new terminal by typing python my_prog.py it works, but I am looking for an interactive way.

In VS Code, the command "Run Python File in Terminal" is executed by default in a "default" "Terminal", which avoids the impact of runtime confusion and reduces the time to find python and file paths at runtime.
If synchronization is required, VS Code currently does not support running two or more terminals simultaneously. You can refer to this link:
Can VSCode automatically run python scripts in two or more terminals at the same time?
If you don’t require synchronization, only need to be in a new independent terminal every time the code is executed, you can set it in "launch.json": "console": "externalTerminal",
Then click Ctrl+F5 to run the code or click F5 to debug the code:

Related

VS Code trying to run .../Activat.ps1 in cmd terminal

I'm using VS Code for a Python project using a virtualenv. I switched my deafult terminal from powershell to cmd as VS Code was not happy executing powershell scripts.
Now when I open a terminal in my project it opens cmd (as desired), but automatically tries tor run .../Scripts/Activate.ps1, which it doesn't like. I want it to run .../Scripts/Activate.bat as we are in cmd. Runnning it manually for now, but would be nice if I didn't have to.
No doubt there is a setting somewhere to change this, but I cannot find it. Any ideas?
This is a problem related to the Python extension, it should be fixed in the last update.
You can get some information from here.

How can I force VS Code to run Python processes in a fresh terminal?

In VS Code (VSCodium to be specific), when I click on "Run Python file in terminal" - the little green arrow top right - the same terminal is used for multiple processes. I'd like to run multiple Python processes in parallel. Currently, the only way is to open a new terminal inside VS Code and run the desired file manually by entering its path. Is it possible to reconfigure the "Run" button so that a new terminal will be opened where the current file that's open in the editor gets started?
According to your description, if you want to automatically open multiple new terminals, it is recommended that you set the way of result output: set "console": "externalTerminal", in the "launch.json" file in the ".vscode" folder. After setting, the result of each debugging will be automatically output in a new cmd window.
Update:
For setting the python run button ("Run python file in terminal"), so that the code can be automatically synchronized to run in different terminals. Currently, VSCode does not support this function. The available method is to manually enter the path of the script as you said.

When to use the terms python shell and python interpreter?

1) I am new to Python and love to learn its core. I have downloaded the python software package and discovered the python.exe application inside. I double clicked it and a balck and white window popped up.
Should I call it a python Interpreter or python Shell?
2) I am learning python online. I came across the terms python tty, python shell and python interpreter. I am satisfied by calling that screen inside the window as a tty(TeleTYpewriter) because we could use only keyboard to work inside and no mouse. But actually that screen has got some intelligence responding to our request. Is python tty an apt term for it?
3) In UNIX, shell is an user interface and command line interpreter, so does python interpreter and python shell are the same.
Python shell lets you use the Python interpreter in interactive mode, just as an OS shell, such as bash, lets you use the OS in interactive mode. You can use the Python interpreter in script mode or batch mode wherein you let the interpreter execute all lines of code in one sequence. It is comparable to writing shell scripts (or batch files in Microsoft Windows).
In your case the screenshot is of a python "shell".
You shouldn't really pay attention to this distinction because in the end everything runs through the python interpreter be it in interactive mode or not.
It is both the python shell and the python interpreter. The shell is where you write your code directly in the CLI, whereas the interpreter is the program that will interpret your code and execute it. Therefore, the interpreter is called in the shell when you write some code, it may also be called when you execute some python code directly from a file.
The customary term for the interactive Python shell is the Python REPL. Many modern interpreters enter a Read-Eval-Print-Loop when you run them interactively, and this term has stuck.
The program which interprets and executes your Python code is the Python interpreter; it can act as a shell, as described above, or run silently and just execute your Python code without any visible user interface of its own, like when you run a script of yours with
python scriptname.py

In VS Code, can one run Python code in an integrated Python terminal like in Spyder?

Currently, in Visual Studio Code (under Windows 10 64bits), at a Python file called path\myfile.py, if one clicks with mouse right-button for context menu and then chooses 'Run Python File in Terminal', an integrated CMD terminal is open and file is automatically run there with:
python.exe path\myfile.py
After the file stops running, one is naturally left at the integrated CMD cursor.
This behavior is quite different, for instance, from what one has with an IDE like Spyder. There, when you run code (e.g. with F5), at the end one is left still at the Python cursor and can access content of variables created when code was run.
Is there a way to achieve a similar behavior in Visual Studio Code?
You can configure VS Code Python extension to use the -i command line option
Described in https://docs.python.org/3/using/cmdline.html#cmdoption-i
You only have to add the setting bellow (inside settings.json file)
"python.terminal.launchArgs": ["-i"],
This will execute the command python.exe -i path\myfile.py.
I don't know if it is a new feature, but I've been using it for while.
If you would like to use the terminal IPython, like in Spyder, you can use a different set of options, as the following:
"python.terminal.launchArgs": ["-m","IPython","-i"],
With these, VS Code will execute the command python.exe -m IPython -i path\myfile.py.
Then, it will run IPython module as a "script" (with -m option), which will use the options -i path\myfile.py, i.e., IPython will run the file and remain opened.
BTW, another thing is: you can run "cells" in Spyder's integrarted terminal (regions of code with #%%). But in VS Code it seems you can't.
I've made a question with a "work around" to run cells of Python files in VS Code Integrated terminal, which is posted Here
Yes. Open a terminal window and it's like a terminal window on your computer. You can type python filepathandname and the python script will execute like it does from the command line.
The closest you can come is to run the code under the debugger and set a breakpoint at the end to pause the exiting of the execution. Otherwise feel free to file a feature request at https://github.com/microsoft/vscode-python.

Is is possible to send code from text editor to existing ipython console

I am starting to learn python with sublime and ipython. They are cool tools and but I want a way to connect them.
I normally have a sublime and a IPython console open. Is there any command that I can run in sublime just send:
runfile('~\someExample.py', wdir='~\myDir')
to the running IPython console?
Thanks!
I edit in geany and use, in ipthon:
run myfile
to load and run myfile.py. ls and cd and pwd are available to check and change the directory.
I save the file from the editor, but I control the run from the ipython console.
If I just need to run the script, I could do a python myfile.py in the editor's terminal window, or maybe via the editor's execute shortcut. But the value in running the code in an existing ipython console is that I can interact with the newly loaded code and variables. I can examine variables, rerun functions with new values, etc.
There is a plugin to do this called SendCode; you can install it directly through the package manager.
As another answer mentioned, you can run some set of shell commands from within ipython, including execution of a file with run python_file.py, as well as other shell commands including cd, mkdir, rm, mv, etc.

Categories

Resources