Let's first talk about what I'm trying to do (using Python 3.x):
As a part of a small VFX pipeline, I'm working on a tool which is supposed to launch programs (e.g. Blender) after setting a couple of environment variables. No issues with this part directly. The issue is the visibility of the console.
There are three requirements that I find conflicting with each other.
Requirement 1: The console of the tool itself will need to be hidden.
Requirement 2: A separate cmd/console needs to be launched for Blender, as the output of this tool needs to be accessible. (For if I work on a script in Blender I need to be able to see errors and prints.)
Requirement 3: The tool itself should be hidden once the Blender launches, and should come back if Blender is closed. (This is so that it can quickly be restarted in case of a crash. A lot of VFX software is incredibly unstable once you are working on heavier scenes.)
Individually I'm able to cover each of the requirements. I'm even able to cover 2 different ones at the same time, but I struggle meeting all 3 of them at once.
(I'm using Tkinter for the UI)
Option number 1:
command = "C:\Program Files\Blender Foundation\Blender 2.91\blender.exe"
ui.withdraw()
os.system(command)
ui.update()
ui.deiconify()
Using os.system will freeze the tool while Blender is running. It will unfreeze and thus also unhide as soon as Blender crashes. But I'm missing the console output. (The tool itself is launched using subprocess.Popen. This is on purpose as I won't need the console of the tool itself, I only want the console to show up once Blender is actually launched.)
Alternatively, I tried:
ui.withdraw()
os.system(f'start cmd /C {command}') # also tried the same command using subprocess.Popen
ui.update()
ui.deiconify()
Launching a console and executing Blender from within it gives me the desired console, but the tool doesn't wait anymore until Blender is closed. It's immediately back to being visible as it considers launching the cmd as the end of the command.
I could use the second method to start the tool itself and then use the first method within it. That solves both Requirement 3 & 4, but it also means the console will be visible before Blender is launched failing requirement #2.
PS: Apologies, if anything is chaotic or overcomplicated. I'm a VFX artist and not a programmer and I only use python now and then. I feel like my knowledge is very imbalanced. It's quite advanced in regards to some topics but has big gaps regarding others.
Related
For a distributed system I have several processes that are required to be launched and I am working on (developing) almost all of them at the same time (modify here, adapt there, ...).
I usually run them in several terminal windows, but would prefer some kind of "process launcher" where I can configure the individual processes in a config file, and then preferrably get the log output, maybe colorized, in a single terminal window.
I started to write something on my own, but have not been happy with the result and wonder if there is not a dedicated tool for that purpose.
Reloading (on file change) would be cool, but I could also use watchmedo for that purpose.
Does someone here have a hint for me?
You may be interested in docker-compose, using it you will be able to predefine multiple python processes easily and run them all together, seeing the logs of each process live and colored, restarting all or part of them, etc...
In jupyter, when an error occurs, we can continuously debug the error with the command %debug. I want to know if there is the similar way in running python script (.py) in the shell.
I know that I can use pdb to make some break points, but I just want to know the way without such a pre-processing (because re-running the code until the error costs a lot of time).
In general, no: it depends on "the shell" that you are running. Jupyter launches with a lot of instrumentation in support of its debugger, assuming that you're using Jupyter because you want those capabilities at the ready.
I presume that you're using some UNIX shell (since you mention pdb); implicitly loading superfluous software is antithetical to the UNIX philosophy.
I think that what you'll need is one of the "after" debugger modes, although that will still leave you without information from just before the error point: those packages cannot do much to trace the history of problem variables.
I have a Python 2.7 script that among others contains the following piece of code:
import spss
columns = []
spss.StartDataStep()
dataset = spss.Dataset()
for column in dataset.varlist:
columns.append(column.name)
spss.EndDataStep()
print columns
When running this code inside a SPSS syntax (so between BEGIN PROGRAM. and END PROGRAM), it runs as expected and I end up with the variables in the active dataset.
However, when running the same code as part of a script (so from Utilities > Run script...) will return me no results.
It looks as if the SPSS session context is not taken into consideration when running a script.
Is there a way around this problem, or am I doing something wrong?
I don't want to run my code as part of Syntax file, I just want to use vanilla Python scripts.
This is, unfortunately, a complicated issue. I don't think Statistics is working as documented. I will take this up with Development.
It appears that in V24, when you run a Python script via Utilities > Run Script (which is the same as issuing the SCRIPT command), your script is connected to the Statistics Viewer process but not to the Statistics backend (the spssengine process), which is where the data live. There are typically three processes running - the stats.exe process, the spssengine process, and, for Python code, the startx process. Your script can issue commands via the spss.Submit api and can use other spss apis, but they go against a new copy of the backend, so the expected backend context is not present.
To get around this, you can run a trivial program like
begin program.
import ascript
end program.
where ascript.py is a Python module on the Python search path. (You could put these lines in an sps file and use INSERT to execute it, too.)
Another way to approach this would be to run Statistics in external mode. In that mode, you run a Python program that uses SPSS apis but the Python program is on top, and no Statistics user interface appears. You can read about this in the Python scripting help.
An advantage of external mode is that you can use your favorite Python IDE to build and debug your code. That's a big advantage if you are basically a Python person. I use Wing IDE, but any Python IDE should work. You can also set up an alternative IDE as your default by editing the clientscriptingcfg.ini file in the Statistics installation directory. See the scripting help for details. With a tool like Wing, this lets you debug your scripts or other Python code even if run within Statistics.
I want to attach to a python script that is called by another program (.exe).
I am developing the python script.
Right now, I am using (Python 2.7)
raw_input("Press Enter to continue and/or attach debugger...")
to make the script wait and attach the debugger.
Is there some convenient way, like C#
Debugger.Break()
(see https://stackoverflow.com/a/105599/586754 ) to directly prompt me to attach with the already open Visual Studio instance? Manually attaching (especially when doing many runs) takes some time. I have done it so often I even know Ctrl+Alt+P to open the attach dialog (then click in program list, hit p for python, click attach..).
There is no such thing currently. Feel free to file a feature request.
I wrote a program that uses the console. Most of the time, the user must see the console informations. For a specific function from command line, I would like to run the script without the console rises. I just don't want see the window but it can be in the task bar. I know I can use extra modules (gui, win32,..) to do that but I would like to use the standard python librairy.
Is it possible to do that?
The program should run on Windows. (python 2.7)
I specify... I know I can use pythonw.exe too. The question then is how to launch the same script with python.exe sometimes and with pythonw.exe (from command line) for a specific function?
Found this question via google, so to answer the question, how to minimize (not completely hide) the console window on Windows when running a Python script:
# Python 3
import ctypes
ctypes.windll.user32.ShowWindow( ctypes.windll.kernel32.GetConsoleWindow(), 6 )
GetConsoleWindow() will return the window handle for the current console.
ShowWindow(hWnd, nCmdShow) will set the properties for the specific window. 6 is SW_MINIMIZE. Click on the link for other parameters.
on windows there are two python executables in your installation, one is "python.exe", which is the one you typically use. There is another called "pythonw.exe" which is for gui programs. It works just like python.exe, but does not display a console at all.
My first idea would be to have a .pyw-script for that specific task, that is launched in this case. Then only the original script's console pops up for a short time.