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
Related
By default, running a .py file through VSCode Code Runner executes the program in the VSCode integrated terminal:
I would like it to do exactly what it does in the integrated terminal, but launch the code through a separate Windows command prompt, like PyCharm.
If I go into settings.json in VSCode, I could change "code-runner.runInTerminal": true, to false, it will execute the code in the output tab instead of terminal tab.
This doesn't help me much.
From settings.json I can change:
to:
This executes the program in the external terminal I want, but it has some serious drawbacks. For one, I have to add input() at the end of my code every time in order for it not to automatically close. The bigger problem is that if I have some kind of logical error in my code, it won't show me any type of error message; just closes the command prompt.
Is there any way to have the code execute in an external Windows command prompt, show all error messages (either in VSCode or terminal itself), and have the terminal require input to close without needing the input() function at the end of my code?
You can open windows terminal/command prompt from the start menu, navigate to your python file and use the same python command to run the python file that you used in VSCode.
[enter image description here][1]
You won't need to add "input()" to your code and you will also get tracebacks.
EDIT: reuploaded picture.
[1]: https://i.stack.imgur.com/qzQz2.png
Attempted to use the commands as suggested by others in the comment, no results or change.I have switched from CommandPrompt to Powershell and want to use the same command I used in Command prompt to open the file I'm working on in VSCode, i.e.:
code .
In Powershell, this doesn't launch VSCode or really do anything noticeable. Would love some insights. Thanks.
Try the below. This will open the current folder on which you triggered powershell.
Start-Process code .
As #Steven already mentioned, it should also work by just typing code on your powershell input.
I would like to include a command to create a 7zip archive withinin a Python script. Since I am working on Windows, I need to pass the command to the powershell console. I am planning to do it with os.system (I am aware that this is not the best way to do it and that I should use subprocess, but I really just need a quick fix and it would not be time effective for me to learn to use a new module in this context).
The following command works if run from the powershell console
&'C:\\Program Files\\7-Zip\\7z' a -mx=0 X:/myarch.zip X:/myarch
So I recreate the same string within python like this:
cmdl = r"&'C:\\Program Files\\7-Zip\\7z' a -mx=0 X:/myarch.zip X:/myarch"
The string is interpreted as follow:
"&'C:\\\\Program Files\\\\7-Zip\\\\7z' a -mx=0 X:/myarch.zip X:/myarch"
Now, if I copy-paste the above string within the powershell console, it runs without problems. However, if I run it within python using os.system(cmdl) I got the following error
"The filename, directory name, or volume label syntax is incorrect"
Why is this the case and how can I fix this issue ?
os.system is meant for executing cmd commands, cmd commands can be ran in powershell maybe after all powershell is a bit advanced but I'm sure that you can't run a cmd command in powershell, henceforth your code is not working.
However a creative solution for executing a powershell command from python(not using python) would be to write your command into a .ps file(powershell script)and then run it using os.startfile()(use this code: os.startfile("script.ps"))
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.
I am using am using Python 2.7 on MacOS and want to use a bash command within a python script.
command = "someProgram --option1 value 1 --option2 value 2"
I had to include the path of this program in my bash_profile in order to run it. I tested so far:
os.system(command)
and
subprocess.check_call(command.split(" "),shell=True)
Neither worked. The latter threw error 127 and the first one only returned 32512. A google search told me that this occurs when the command is not known.
If I now start this command within the terminal everything works perfectly fine.
Do I have to include something such that python can find this command? Why is this behavior?
With shell=True the cmd needs to be a string.
subprocess.check_call(command, shell=True)
where command is of type str
Thanks for your help. The final solution is kind of stupid. I started spyder via the anaconda GUI. If I do so the above code does not work.
If I run this directly via the console or start spyder via the console everything is fine. It seems that the bash_profile is not loaded when spyder is loaded but requires the console to do so