How does PyCharm run Python scripts? - python

A few days ago I was getting issues trying to run Tensorflow models with CUDA enabled and for a long time I couldn't resolve in large because PyCharm displayed completely unhelpful message
"Process finished with exit code -1073740791 (0xC0000409)"
I then launched VSCode and ran the same code in PowerShell and got a nice and extensive error message which allowed me to resolve all issues within half hour (same when running in cmd too). Other outputs are also somewhat different. So this makes me assume that PyCharm runs scripts in some type of its own terminal rather than relying on cmd or PowerShell.
Does anyone know if this is the case?

So this makes me assume that PyCharm runs scripts in some type of its own terminal rather than relying on cmd or PowerShell.
Not necessarily. Just because PyCharm displays a custom error message doesn't mean it doesn't rely on cmd. Here's a Python script that simulates what PyCharm does using the cmd:
# So we can launch a process from Python
import subprocess as sp
# Launches a Python process for a specific file
# `stdout=sp.DEVNULL` suppresses the process output
# so that's why there is no detailed error message
child = sp.Popen(["python", "file.py"], stdout=sp.DEVNULL)
# Start the Python process
process = child.communicate()[0]
# Returns the process exit code
# If the process finishes without errors, it'll return 0
# otherwise, it'll return a "random" value
exit_code = process.returncode
# Displays to stdout the completely unhelpful message
print(f"Process finished with exit code {exit_code} ({hex(exit_code)})")
Either way, here's what PyCharm says:
Initially, the terminal emulator runs with your default system shell, but it supports many other shells such as Windows PowerShell, Command Prompt cmd.exe, sh, bash, zsh, csh, and so on.

Related

Software launched with os.system or subprocess.Popopen closes as python shuts down

I am trying to launch an android emulator from Python. I have tried the following code:
os.system('C:\\Nox\\bin\\Nox.exe -clone:Nox')
subprocess.Popopen('C:\\Nox\\bin\\Nox.exe -clone:Nox')
The emulator launched by either code closes as soon as python code is terminated. However, when I run the code ('C:\\Nox\\bin\\Nox.exe -clone:Nox') in Win10 terminal, the emulator doesn't close when the terminal is closed.
How can I keep the emulator running when python code terminates? I do not want to keep python code running.
I don't have a Windows machine to try this on, but in Ubuntu the following did it for me:
import subprocess
subprocess.Popen('<your command string>', shell=True)
So in your case:
import subprocess
subprocess.Popen('C:\\Nox\\bin\\Nox.exe -clone:Nox', shell=True)
Note there is a parameter creationFlags with values that seem of interest (https://docs.python.org/3/library/subprocess.html#windows-constants), however hopefully shell=True will suffice.
Do note the strong warnings in the documentation around opening a process with shell=True where the process being run depends upon some user input!

Running a python script in a Windows 10 powershell

I have a little python script that takes a single filename as a command line argument and writes a converted file. Not rocket science, it's 10s of lines long.
I can run that on windows from a cmd prompt simply by typing the name of the script. So for example:
C:\> CD \wheremyscriptis
C:\wheremyscriptis> myscript.py
and it runs fine. Without any arguments it spits out a little Usage message. Quite conventional.
Now we're using Powershell more and more and the first thing I notice in powershell is it won't run at all without an explicit directory, so in the above example I'd need to type .\myscript.py.
That's odd but you could get used to it, not a crisis.
But what happens now is the script runs in another window, which flashes up and disappears before youc an read the usage message.
Given the behavior is inconsistent across these contexts, what can we do inside the script to make the powershell context more usable. Is there a way to detect if we're running in some window that powershell threw up (which is itself weird) and then if so, pause before exiting to give a user a chance to read the usage message before the window disappears?

Viewing errors on ogr2ogr - python subprocess check_output returned non-zero exit status 1

I am getting this error on running a subprocess.check_call that runs an ogr2ogr command.
I have put on error trapping but can't see the error details and the cmd window closes without me being able to see the problem.
How can I trace the problem?
The screen grab shows the working code (when typed in), the python script and the output of python shell.
One issue maybe the ' in the python generated code. The command is built based on https://gis.stackexchange.com/questions/154004/execute-ogr2ogr-from-python/246667 where each option is wrapped in "[OPTION]",
Figured it out with a colleague...
Needed to
Load this in a batch shell that had the paths to ogr
Trust that subprocess takes care of the extra "EPSG:23555" when required and not do "\"EPS...\"" to get the code to be the same as what runs in OSGEO4W Shell.
Running the .py file from powershell rather than IDLE/Pyscripter

Running multiple Python scripts

I would like to create a simple Python program that will concurrently execute 2 independent scripts. For now, the two scripts just print a sequence of numbers but my intention is to use this program to concurrently run a few Twitter streaming programs in the future.
I suspect I need to use subprocess.Popen but I cannot quite get my head around what arguments I should put in there. There was a similar question on StackOverflow but the code provided there (pasted below) doesn't print anything. I will appreciate your help.
My files are:
thread1.py
thread2.py
import subprocess
subprocess.Popen(['screen', './thread1.py']))
subprocess.Popen(['screen', './thread2.py'])
Use supervisord
supervisord is process control system just for the purpose of running multiple command line scripts.
It features:
multiple controlled processes
autorestarting failed runs
log stdout and stderr output
starting scripts in order (using priority)
command line utility to view latest log output, stop, start, restart the processes
This solution works only on *nix based systems, it is not available on Windows.
As wanderlust mentioned, why do you want to do it this way and not via linux command line?
Otherwise, the solution you post is doing what it is meant to, i.e, you are doing this at the command line:
screen ./thread1.py
screen ./thread2.py
This will open a screen session and run the program and output within this screen session, such that you will not see the output on your terminal directly. To trouble shoot your output, just execute the scripts without the screen call:
import subprocess
subprocess.Popen(['./thread1.py'])
subprocess.Popen(['./thread2.py'])
Content of thread1.py:
#!/usr/bin/env python
def countToTen():
for i in range(10):
print i
countToTen()
Content of thread2.py:
#!/usr/bin/env python
def countToHundreds():
for i in range(10):
print i*100
countToHundreds()
Then don't forget to do this on the command line:
chmod u+x thread*.py
You can also just open several Command Prompt windows to run several Python programs at once - just run one in each of them:
In each Command Prompt window, go to the correct directory (such as C:/Python27) and then type 'python YourCodeNo1.py' in one Command Prompt window, 'python YourCodeNo2.py' in the next one ect. .
I'm currently running 3 codes at one time in this way, without slowing any of them down.

os.system('exit') in python

My friend is in a macOS environment and he wanted to call os.system('exit') at the end of his python script to make the terminal close. It doesn't. This doesn't surprise me but I would like to know what exactly is going on between the python script and the terminal when this call is made.
In my mental simulation the terminal should have to tell you that there are still running jobs, but that doesn't happen either.
As a side question : will some less common terminals close when a process calls this?
read the help:
Execute the command (a string) in a subshell.
A subshell is launched, and exit is run in that subshell.
To exit the enclosing terminal, you have to kill the parent. One way to do it is:
os.system("kill -9 %d"%(os.getppid())
The system function starts another shell to execute a command. So in this case your Python scripts starts a shell and runs "exit" command in there, which makes that process exit. However, the Python script itself, including a terminal where it is running, continues to run. If the intent is to kill the terminal, you have to get the parent process ID and send a signal requesting it to stop. That will kill both Python script and a terminal.
Remember that system first spawns/forks a sub-shell to execute its commands. In effect, you are asking only the sub-shell to exit.

Categories

Resources