Run terminal inside VS Code for python script? - python

I'm on Linux Mint running VSCode and I was somehow able to run a terminal not as a separate window but right below an open Python file. Seems to be easy on Win/OSX (Ctrl/Cmd+J and select Terminal tab) but not specifically a feature that I can choose when I'm on a Linux machine. Any special keys to bring it back?

In general, Ctrl-` opens the integrated terminal in VS Code. Otherwise look under the View menu for the Integrated Terminal option. If you're looking for Python-specific options, there are Run Python File in Terminal and Run Python Selection/Line in Terminal commands from the command palette.

Related

How do i run python file in cmd from vscode

I have a python program which prints long outputs. When i try to run that file in vscode, its interactive window isn't enough to view full output. So is there any way to run python file in cmd from VSCODE?
If you are running windows, VSCode uses Powershell as your terminal by default. If you want to use the command prompt instead, hit ctrl+shift+p, type Shell into the command pallet, select Terminal: Select Default Shell, and change it to Command Prompt. I am not sure this will fix your problem as I think Powershell should display just as much output as the CMD, but if you want to try switching terminals, that will do it. Another option is to try to run it natively in CMD or Powershell, rather than using the VSCode integrated terminal. That might be better if changing terminals doesn't help.
As #Jeremiah said, you can also just run your script with the Cmd prompt, without using vs code. Let's say you have the file 'test1.py' saved as C:\Users\bcubrich\Documents\test1.py, and your python env .exe is saved in C:\python27\ArcGIS10.5\python.exe. I just wrote script that had this in it:
print('worked')
Then just input this into the Cmd prompt
C:\python27\ArcGIS10.5\python.exe C:\Users\bcubrich\Documents\test1.py
And it printed
worked
to the console.
More on running python through cmd console here:
http://www.cs.bu.edu/courses/cs108/guides/runpython.html

Visual Studio Code; how to open two Python shells side-by-side

I can open a terminal with command "Python: Create Terminal" and that works great. It is using my selected Python Interpreter that I created with pipenv. But I want to create two of these terminals side-by-side.
If I split the terminal, a new bash terminal starts that only knows about the system installed Python.
If I create another Python terminal, it opens by itself. The two terminals are running but I can only display one or the other
Is there a way to have two Python terminals open and displayed at the same time?
I'm on windows10.
Not from the Create Terminal command, no. But you can copy and paste the shell command that gets executed by Create Terminal in the second shell and it will lead to the same result.
We also have an open feature request that would solve this by automatically executing the appropriate command on every opened terminal. Feel free to leave a :+1: reaction to the issue to let us know you would like to see the feature implemented.

Python curses Redirection is not supported

I am trying to use Curses in PyDev in Eclipse in Win7.
I have installed Python 3.2 (64bit) and curses-2.2.win-amd64-py3.2. When I input the following testing codes into PyDev:
import curses
myscreen = curses.initscr()
myscreen.border(0)
myscreen.addstr(12, 25, "Python curses in action!")
myscreen.refresh()
myscreen.getch()
curses.endwin()
It did not show any syntax error, so I think the curses was installed correctly.
However, when I ran it as Python Run, the output showed: Redirection is not supported. I do not know where this problem comes from. I googled a lot but can't find related information.
Recent PyCharm versions (I am currently running 2017.2, not sure when this option was added, or if it has been there the entire time) have the option "Emulate terminal in output console". Curses works with this option checked.
You cannot expect to use curses with a non-terminal.
Probably you get this because you are running the script from inside an IDE, like PyCharm or any other.
All IDEs do provide consoles that are not terminals, so that's where the problem comes from.
For a Pycharm user the solution given by codeape works fine :
Snapshot
You can't use any IDE to run python files with the curses package. I used to run in pycharm and naturally couldn't run.
Change to the command line to run:
for testing follow my following steps
on desktop open notepad and copy paste the code and save it as filename.py
open command line change directory to desktop use below command cd Desktop and hit enter type python example.py and hit enter, your program will definitely run
My workaround is to create a Run Configuration that calls a curses script. The little overhead is worth not having to switch to the terminal and manually run the script hundreds of times a session. I use Intellij but I imagine the process should be similar in PyCharm.
The desired result is the convenience of a button to run the script:
First create a script that calls the entry script, for instance:
ptyhon name-of-script.py
Then, to create a configuration for each script:
Go to Edit configuration.
Click the plus button and add a Shell Script.
Enter the path to a shell script.
Here is a picture of a directory with a couple of sample scripts.
I use this process to view my progress. My curses scripts are very modest so fortunately I can live without a debugger.

Can we know if a Python script is launched from Windows or a textual terminal?

I use the Windows version of Python. I have a Python script using Pyside (nothing complicated, a kind of "hello world").
When I click on my script file or if I launch it from a command line, it executes perfectly and I have a GUI appearing.
However, I would like to avoid having a GUI if the script is launched from a textual terminal (cmd.exe, cygwin, ...). A kind of script which automatically knows if it should have a GUI output or a textual output.
Is there an easy and simple way to do that? I want to be able to do that with the Windows version of Python (not the one coming with Cygwin packages).
An obvious way would be to add a kind of "--no-gui" parameter when I launch the script from a textual terminal, but I wonder if Python (or some Python libraries) already provide tools for that.
Moreover I have an SSH server (Cygwin-base) on this computer, I can execute the script at distance but no GUI appear (of course) and I have no error message. It is a case where it is very interesting to know if the script failed because of the lack of Windows graphical support or if the script should adapt its output for a textual terminal.
I know that you can run file as .py file or as .pyw file. The second option is used for GUI applications and it does not open the console window. To distinguish these to two cases you can check isatty method of sys.stdout.
import sys
if sys.stdout.isatty():
# .py file is running, with console window
pass
else:
# .pyw file is running, no console
pass
EDIT:
I tried to run that with putty+ssh on linux box - it returns True.
I tried to use msys bash shell on windows box - it returns True (.py file)
I tried to use cygwin bash shell with cygwin python - it returns True (.py file)
Unfortunately, I have no possibility to try putty + windows cygwin ssh server.

Running Python from the Windows Command Line

How do I run a Python file from the Windows Command Line (cmd.exe) so that I won't have to re-enter the code each time?
Wouldn't you simply save your Python code into a file, and then execute that file using Python?
Save your code into a file called Test.py.
And then run it?
$ C:\Python24\Python.exe C:\Temp\Test.py
If you don't want to install an IDE, you can also use IDLE which includes a Python editor and a console to test things out, this is part of the standard installation.
If you installed the python.org version, you will see an IDLE (Python GUI) in your start menu. I would recommend adding it to your Quick Launch or your desktop - whatever you are most familiar with. Then right-click on the shortcut you have created and change the "Start in" directory to your project directory or a place you can mess with, not the installation directory which is the default place and probably a bad idea.
When you double-click the shortcut it will launch IDLE, a console in which you can type in Python command and have history, completion, colours and so on. You can also start an editor to create a program file (like mentioned in the other posts). There is even a debugger.
If you saved your application in "test.py", you can start it from the editor itself. Or from the console with execfile("test.py"), import test (if that is a module), or finally from the debugger.
If you put the Python executable (python.exe) on your path, you can invoke your script using python script.py where script.py is the Python file that you want to execute.
Open a command prompt, by pressing Win+R and writing cmd in that , navigate to the script directory , and write : python script.py
A good tool to have is the IPython shell. Not only can it run your program (%run command), but it offers also many tools for using Python interactively in an efficient manner (automatic completion, syntax coloring, quick access to the documentation, good interaction with Matplotlib,…). After you install it, you'll have access to its shell in the Start menu.
You need to create environment variables. Follow the instructions here: http://www.voidspace.org.uk/python/articles/command_line.shtml#environment-variables
In DOS you can use edit to create/modify text files, then execute them by typing python [yourfile]

Categories