Run Python file directly upon command line start of Thonny, without pressing F5?

3,616 views
Skip to first unread message

thonny_starter

unread,
Jan 24, 2022, 6:32:29 PM1/24/22
to thonny
Hi!
I want to run a Python file IN THONNY !!! directly from a command line on Raspberry Pi.
I can type
/bin/thonny /my_directory/my_python_file.py
or
python3 -m thonny /my_directory/my_python_file.py
and both start Thonny and open my_python_file.py. But I still have to press F5 to run my_python_file.py. Please, is there a way to run my_python_file.py IN THONNY (not in python3) directly upon one Raspberry Pi command line, without having to press F5?
(The reason why I want to do this is because I have a Python file which runs well under Thonny but does not run under python3 even though python3, tkinter, numpy and gcc have identical versions in both cases.)
Thank you for your help!

Aivar Annamaa

unread,
Jan 25, 2022, 2:10:56 AM1/25/22
to thonny
Hi!

Thonny doesn't have such feature and most likely never will.

By default, Thonny on RPi uses the same Python interpreter as you get when simply calling python3. Perhaps you rely on the interpreter process to remain active after your script ends? In this case you could try adding "-i" switch to your command line: 

python3 -i /my_directory/my_python_file.py

Best regards,
Aivar
Message has been deleted

Aivar Annamaa

unread,
Jan 25, 2022, 8:47:18 AM1/25/22
to thonny
I suggest comparing the output of following script both via Thonny and via python3 on cmd-line:

import sys
import os

print("Exe:", sys.executable)
for key in sorted(os.environ):
    print(key, "=", os.environ[key])


best regards,
Aivar


On Tuesday, January 25, 2022 at 12:43:29 PM UTC+2 thonny_starter wrote:
Hi Aivar, thank you for the fast answer!
The -i did not solve the problem.
Seemingly, in my case Thonny uses a better Python interpreter than python3.
To calculate and display a color pixel graphic (ppm PhotoImage) I need cv2 and numpy.
My program test_GUI.py runs under thonny but not under seemingly equal python3.
Do you happen to have any idea to solve my problem, please?
Thank you,
thonny_starter

-------------------------------
# This is my program test_GUI.py
# I have left out all my needed cv2 and numpy code lines because the problem exists even without these!
from tkinter import *
import numpy as np
import cv2
win = Tk()
win.mainloop()
-------------------------------

On two identical
Raspberry Pi 4 Model B Rev 1.4
with
Distributor ID: Raspbian
Description: Raspbian GNU/Linux 10 (buster)
Release: 10
Codename: buster
and Kernel
5.10.63-v7l+
I get the following:

test_GUI.py runs under thonny!

But with the raspi shell command
sudo python3 test_GUI.py
I get this python3 error message:
RuntimeError: module compiled against API version 0xe but this version of numpy is 0xd
Traceback (most recent call last):
File "test_GUI.py" , line 3, in <module>
import cv2
File "/usr/local/lib/python3.7/dist-packages/cv2/__init__.py", line 5, in <module>
from .cv2 import *
ImportError: numpy.core.multiarray failed to import

This is strange, because thonny and python3 seem to be configured exactly the same (Python 3.7.3, Tk 8.6.9, numpy 1.21.5), which I have verified.

I had to undergo all the following steps in order to successfully import cv2 in Thonny (but unfortunately not yet in python3):
pip3 install --upgrade numpy
# (installs numpy-1.21.5)
sudo apt-get install python3-tk
# (installs tkinter 3.7.3-1)
sudo pip3 install opencv-contrib-python==4.5.3.56
# (because "sudo pip3 install opencv-contrib-python==4.5.4.60" and the standard "sudo pip3 install opencv-contrib-python" hung up)
sudo apt-get install libhdf5-dev libhdf5-serial-dev libhdf5-103
sudo apt-get install libqtgui4 libqtwebkit4 libqt4-test
sudo apt-get install libatlas-base-dev
sudo apt-get install libjasper-dev
wget https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py

thonny_starter

unread,
Jan 25, 2022, 1:55:29 PM1/25/22
to thonny
Hi Aivar,
I found a lot of environment variable differences but the solution simply this:
sudo python3 test_GUI.py # THIS FAILS!
python3 test_GUI.py # THIS WORKS, LIKE IN THONNY!
Your helpful answer triggered me to find this.
Thank you very much!
You see, my user name is adequate ...
Best regards!
Reply all
Reply to author
Forward
0 new messages