Python3 GUI script does not work when double clicked - linux

My GUI script that is a PyQt5 file (.pyw extension) does work when running on my IDE with a build configuration that tells the compiler to run the script with python3:
And it also works when i tell to the regular terminal on Linux to run same script with python3 like this:
When runned with the default python (python2.7) on a regular terminal it tells: ImportError: No module named PyQt5.QtWidgets.
My code does it have these lines on the start to tell that is a python3 script like: #!/usr/bin/python3 or #!/usr/bin/env python3 (I have python3 installed).
When double clicked on the Linux Mint File Explorer the cursor turns crosshair and nothing happends, with the terminal option, same happends and a empty terminal shows. Im talking these options
I guess Linux Mint still runs the scripts with python2.7 even when I added the bash lines to tell
Someone knows why the lines:
#!/usr/bin/python3
#!/usr/bin/env python3
doesnt work when just double click?
I want to run the script from the Linux File Explorer without the need of an IDE or using the terminal.

Try chmod +x file.py and run it in terminal by using ./file.py also try lunching the file from a different path, like python3 ~/path/to/file.py and see if the error persists

Related

Running two shell commands using Python 3.6 on Ubuntu 18.02

In order to get some software running I need to 1. Run a script that will execute a remote license manage, 2. Execute a shell script to start the software. I can do this by opening a command window in the directory with the rlm , and then type ./rlm to run the Linux executable. Then I can go into the directory that contains the shell script, open a terminal in that location and run ./myshell.sh. This opens the GUI for my software.
I would like to run these steps using a single Python script. I have tried:
#change the working directory...
os.chdir(r'/opt/mysoftwarelocation')
#confirm location change...
print(os.getcwd() )
#run ./rlm...
os.system('./rlm')
At this point I can see from a python terminal that the rlm is running.
I would then like to run the code below to run the shell script...
os.chdir(r'/opt/mysoftwarelocation/sumsubdirectory')
print(os.getcwd() )
os.system('./some.sh')
Unfortunately, after os.system('./rlm') finishes the script stalls and will not execute further and without an errors.
How to I get the second part of my script to run within a single Python script?
Have you tried to run the rlm command in the background?
subprocess module gives a nice interface for that

Environment variable exported using bash script not accessible in python

I have two files :
run.sh
train.py
Content of both file
in run.sh
export TRAINING_DATA=test1
export FOLD=test2
python.exe train.py
in train.py
from os import environ
print(environ.get("TRAINING_DATA"), environ.get("FOLD"))
when I run the bash file through command line
bash run.sh
Instead of getting test1 & test2, I am getting None, None
Both the files are placed in the Scripts folder of my virtualenv. I am using Windows 10.
The issue you are hitting is that you are running the windows version of python.exe rather than the linux/bash version.
In the run.sh i see that you are refering to python.exe, this is the windows exe and as such when it runs it will not be in the same shell as the .sh script used to invoke it.
you can prove this to yourself by doing the following from within WSL
➜ which python.exe
/mnt/c/Python27/python.exe
➜ which python
/usr/bin/python
So as long as you have python installed in WSL you can simply swap out the .exe in the .sh script so that you are using the bash/linux version thus:
export TRAINING_DATA=test1
export FOLD=test2
python train.py
You should try disconnect and log back in on the session.
Happened to me and it's how I solved it.
On my side it was on a ssh console.
Have a great day/night-

Open terminal in XFCE from script and activate python virtualenv

I try to open terminal in XFCE and activate python3 virtualenv using the following line in a bash script:
xfce4-terminal --working-directory=$HOME/path/to/project --maximize \
-e 'bash -c "source $HOME/path/to/project/venv/bin/activate; bash"'
The strange thing is that the virtualenv gets kind of activated since:
which python
shows the correct path to the virtualenv directory and the project seems to be working fine.
However I don't see the (venv) to the left from the shell prompt. Moreover, when I enter deactivate it complains that no such command can be found.
Is there a proper way to solve this problem?
I created a bash shortcut for this in my /Users/username/.bash_profile (I use mac; on linux use Users/username/.bashrc instead).
function pcd() {
cd /Users/username/Code/"$1"_env/"$1"
source ../env/bin/activate
atom -a .
}
Where project_env is the root folder, which contains the venv, and the project folders (project folder is where code goes)
to execute this simply call pcd project
I don't know much about bash, so I can't really tell you why your code doesn't work.
Also, make sure you open a new terminal window after saving this.

Raspberry pi - startup tkinter script issue

I made a tkinter script (python3) which work perfectly when I launch it manually from the terminal.
I wanted to start it on launch so I modified the /etc/profile with :
sudo python3 /home/pi/script/main.py
The thing is that the script is launched but I only got a white screen as if my tkinter GUI wasn't working.
Does someone have an explanation ?
Okay I found the problem :
The tkinter script is looking for the ressources (images, etc.) in the folder in which it is executed.
By running python3 /home/pi/script/main.py tkinter will look for the ressources in the folder in which we execute from (as default it is the home directory (~)).
Tkinter didn't find an image so it stoped on the error (as any python script do)
I just had to change the directory with cd as so :
cd /home/pi/script
sudo python3 main.py

LXDE .desktop file permission issue

I’m trying to run a Python script via a desktop icon/shortcut/launcher on a Raspberry Pi using Raspbian and LXDE. I have to use a desktop launcher since the script has to be started via a tiny touchscreen only accepting left-clicks and without a keyboard.
I think that I have a permission issue since the script fails at a point where it should execute some system commands which require root.
The script works if I do the following:
Open a terminal
Enter sudo python3 program.py
I tried to replicate this behavior with a .desktop file using the following config:
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Exec=sudo python3 program.py
Terminal=true
Icon=path/to/icon.png
Name=Program Launcher
I suppose I missed something obvious, but since I don’t work with Linux usually I’m a bit lost here.
Edit: The problem was not a missing permission but an incorrectly assumed working directory and is now solved. Sorry for my confusion.
In detail: during development the script was started from its own folder while the desktop launcher used /home/pi as working directory. In general that's not a problem, but in the script a hardcoded path was used and a required file not found when using the launcher. The real problem was sloppy swallowed and the status code of a permission issue returned. So it was simply a bad code issue.
As docs for desktop entries says:
The Exec key must contain a command line. A command line consists of
an executable program optionally followed by one or more arguments.
The executable program can either be specified with its full path or
with the name of the executable only. If no full path is provided the
executable is looked up in the $PATH environment variable used by the
desktop environment. The name or path of the executable program may
not contain the equal sign ("="). Arguments are separated by a space.
It leads to conclusion, that maybe python3 is not within $PATH used by the desktop environment. Try to check full path of your python3 and sudo with:
whereis python3
whereis sudo
on my Archlinux it gives me /usr/bin/python3 and /usr/bin/sudo. Try to modify your Dekstop entry to something like:
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Exec=/usr/bin/sudo /usr/bin/python3 program.py
Terminal=true
Icon=path/to/icon.png
Name=Program Launcher
Let me know if this helps.

Resources