Keep terminal open when running Python GUI application from bash script - python

This is probably a very simple question, but I just can't figure it out. I've coded a Python GUI application (uses PyQT), and all I want to do is to launch it with an executable script. I'm still debugging the application, so it'd be nice if the terminal stays open to see any errors/print statements/exceptions thrown.
This is what I've got in my script:
#!/bin/sh
x-terminal-emulator -e cd dirname && python GUIapp.py
It successfully runs the Python application, but once the application loads, the terminal automatically closes. The application continues to run after the terminal closes.
I know I can open a terminal and then simply type in "cd dirname && python GUIapp.py", but I'm lazy.
What am I missing here?

Use --hold or --noclose option. They have the same function with different name.
So change script to
#!/bin/sh
x-terminal-emulator --noclose -e cd dirname && python GUIapp.py
If you are looking for a command option, always check --help. In this case it gives you the needed information.
user#host:~/projects$ x-terminal-emulator --help
Usage: x-terminal-emulator [Qt-options] [KDE-options] [options] [args]
Terminal emulator
Generic options:
[...]
Options:
[...]
--hold, --noclose Do not close the initial session automatically when it ends.
[...]

Related

How could I use python to open gnome-terminal and then run python commands in a multiline manner?

I am attempting to get a subprocess call that will open a gnome-terminal and in that terminal enter python then execute some python commands and imports without the user needing to type them out.
I'm working on on some automated terminal opening code that will open a gnome-terminal window using subprocess.call (Open new gnome-terminal and run command) also (Python syntax to open gnome-terminal and execute multiple commands)
My end goal is to open up a gnome-terminal window and with the same script that opened the gnome-terminal, enter the command to use python. And then in python import a package and run it.
My current usage is:
subprocess.call(['gnome-terminal', '-e', "python client.py"])
However what Im trying to get to is an importable package that I can open several gnome terminal windows for that will call different objects from a pypi package effectively doing the same thing that call client.py would do with the files. This doesnt work with packages installed in pip however.
What I want to do is something along the lines of:
subprocess.call(['gnome-terminal', '-e', "python && import <package> && c = <package>.obj.func()"])
So that a terminal would open and enter python, import the package I want, then call something from it, but all as instructed by a python file
This doesnt appear to work as multiline scripting works for stuff like bash scripting but doesnt seem to work when trying to enter commands after python has been entered.
Any advice would be greatly appreciated
I don't have Gnome Terminal installed, but if you can get that to start Python correctly, then you can use Python's -i flag to run a set of commands or a script.
The two usages are as follows:
python -i path/to/my/script run the script then enter the interpreter
python -i -c "# Some Python commands" run the command(s) then enter the interpreter
For example:
$ python -i -c "import this"
[poetry]
>>>
# Ready for input!

Batch Script not running completely (but was working previously) - how to see errors?

I've got a batch script that is pretty straight forward:
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login -i -c "cd C:\python-script && python run.py"
I've not done a lot with batch scripts before, but here I can tell it's just opening up git bash interactively, then running the command to CD in a directory and run python run.py
When I run this .bat from a cmd prompt, git bash opens up ever so briefly and then immediately closes but the script is never fired off.
Any way I can stop that script temporarily so I can see any error the git window may be throwing?
In order to allow the pause to display in the current window, we can test by removing the start command to execute it in the local window. Also as mentioned in comments, if python is installed and it does not find it, it would require you to specify full path to it which should then work:
"%ProgramFiles(x86)%\Git\bin\sh.exe" --login -i -c "cd C:\python-script" "C:\python37\python.exe" run.py

Bash script: Open Python, execute some lines, then let user take control

I have a Python program, A.py, that creates binary data upon completion. To help users analyze the output, I want to add a small script, B.sh, to the output directory that fires up a Python console and executes some commands, C, that load the data and prepare them such that a user sees what is available. After executing C, the script B.sh should keep the Python console open.
First attempt at B.sh:
I figured out that
#!/bin/sh
xterm -e python
opens a Python console and keeps it open but doesn't execute anything within that console.
Second attempt at B.sh:
I figured out that
#!/bin/sh
xterm -e python -i C.py
executes C.py (I'd prefer not to have to write an additional file for the startup commands, but I could live with that) and keeps the window open, but doesn't show what was done. More specifically, the user would be presented with the outputs of C, but not the command that were used to achieve the outputs.
Instead,I'd like the user to be presented with a console like this:
>>> [info,results] = my_package.load(<tag>)
>>> my_package.plot(results)
>>> print(info)
<output>
>>> my_package.analyze(results)
<output>
>>>
Save this in a file called demo.tcl
#!/usr/local/bin/expect -f
# Spawn Python and await prompt
spawn /usr/local/bin/python3
expect ">>>"
# Send Python statement and await prompt
send "print('Hello world!')\n"
expect ">>>"
# Pass control to user so he can interact with Python
interact
Then make it executable with:
chmod +x demo.tcl
And run with:
xterm -e ./demo.tcl
In the picture, you can see I went on after the "Hello world" to print the system version info.
Your paths for Python and expect may be different, so check and alter to suit.
For anyone who happens to be using macOS (a.k.a. OSX), you can install expect with homebrew as follows:
brew install expect
And, since Macs don't ship with X11 any more, rather than install XQuartz and run xterm, you can start a new Terminal and run the Python in there quite simply with:
open -a Terminal.app demo.tcl
As suggested by user #pask I simply printed the commands before executing them.
Furthermore, I added a -c switch to be able to put the Python commands directly in B.sh instead of having to write a file C.py
Here is the B.sh I am using now:
#!/bin/sh
xterm -e python -i -c "print('>>> import my_package');import my_package;print('>>> [info,results] = my_package.load()');[info,results] = my_package.load()"

Startup shell script not producing output

I am working with Ubuntu 16.04 on an NVIDIA TX2 system and have a shell script launcher.sh containing the following commands:
#!/bin/sh
cd /home/nvidia
sudo python test.py >> /home/nvidia/test_output.txt,
basically implying to store the output of test.py in test_output.txt.
The test.pyhas one line: print "Testing startup script!"
I want this script (and more complicated scripts later on) to run automatically when the system boots up. To this end, I added it to the Startup Applications with the command /home/nvidia/launcher.sh and rebooted the system. The file test_output.txt is indeed created but there is no output written in it. I tested the script from the terminal and it works fine.
How do I make this to work from Startup Applications?
Init scripts often don't have the complete environment initialized, e. g. PATH. In order to be executed you better provide the complete paths to commands.
On the other hand sudo is unnecessary here, as you obviously don't do anything that needs root permissions. If you need it, be aware that sudo is asking interactively for a password, which stalls the execution of the script, if the command you try to execute with it isn't explictely permitted in /etc/sudoers.
Provided test.py is located in /home/nvidia, and python in /usr/bin, the script should read
#!/bin/sh
cd /home/nvidia
/usr/bin/python test.py >> test_output.txt
And if you want to add error output to the outfile, which may be useful for debugging, make the last line
/usr/bin/python test.py >> test_output.txt 2&>1
or for a separate error log
/usr/bin/python test.py >> test_output.txt 2> test_error.log

How to run python script at startup

I'm trying to make a Python script run as a service.
It need to work and run automatically after a reboot.
I have tried to copy it inside the init.d folder, But without any luck.
Can anyone help?(if it demands a cronjob, i haven't configured one before, so i would be glad if you could write how to do it)
(Running Centos)
run this command
crontab -e
and then add
#reboot /usr/bin/python /path/to/yourpythonscript
save and quit,then your python script will automatically run after you reboot
There is no intrinsic reason why Python should be different from any other scripting language here.
Here is someone else using python in init.d: blog.scphillips.com/posts/2013/07/… In fact, that deals with a lot that I don't deal with here, so I recommend just following that post.
For Ubuntu Variant:-
open the /etc/rc.local file with:
nano /etc/rc.local
add the following line just before the exit 0 line:
start-stop-daemon -b -S -x python /root/python/test.py
or
Give absolute path of your command i.e
nohup /usr/bin/python2 /home/kamran/auto_run_py_script_1.py &
The start-stop-daemon command creates a daemon to handle the execution of our program. The -b switch causes the program to be executed in the background. The -S switch tells the daemon to start our program. And the -x switch tells the daemon that our program is an executable.
To check and Run
sudo sh /etc/rc.local

Categories