Elmit
Posts: 3
Joined: Thu Jul 25, 2019 5:51 am

How to distinguish that the python program is executed on a RPI?

Fri Oct 04, 2019 11:50 pm

I am developing Python scripts on different machines, including RPI.

Is there a way to distinguish on which machine I am working, so that I load the right modules?

E.g. PySimpleGUI uses this:

Code: Select all

#!/usr/bin/env python
import sys
if sys.version_info[0] >= 3:
    import PySimpleGUI as sg
else:
    import PySimpleGUI27 as sg
How would I write above to load RPI modules, like camera, GPIO.rpi .... ?

pfletch101
Posts: 634
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: How to distinguish that the python program is executed on a RPI?

Sat Oct 05, 2019 4:50 pm

Elmit wrote:
Fri Oct 04, 2019 11:50 pm
I am developing Python scripts on different machines, including RPI.

Is there a way to distinguish on which machine I am working, so that I load the right modules?

E.g. PySimpleGUI uses this:

Code: Select all

#!/usr/bin/env python
import sys
if sys.version_info[0] >= 3:
    import PySimpleGUI as sg
else:
    import PySimpleGUI27 as sg
How would I write above to load RPI modules, like camera, GPIO.rpi .... ?
If I'm not mistaken, the code that you quote is checking whether it is running under Python 2.x or Python 3.x, not determining what hardware it is running on. To do the latter, probably the easiest approach would be to place files in a known location on all your systems with names corresponding to the 'set' of modules that you want to load. Your scripts can then check for the existence of the 'flag' file(s), and load the appropriate modules.

Andyroo

Re: How to distinguish that the python program is executed on a RPI?

Sat Oct 05, 2019 5:00 pm

You could look at /sys/firmware/devicetree/base/model to see what you are running on but how you would then dynamically select the include modules I have no idea...

hippy
Posts: 9173
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: How to distinguish that the python program is executed on a RPI?

Sat Oct 05, 2019 7:14 pm

Elmit wrote:
Fri Oct 04, 2019 11:50 pm
I am developing Python scripts on different machines, including RPI.

Is there a way to distinguish on which machine I am working, so that I load the right modules?
There is 'os.name' and I am sure others which can give you information about what the code is being run on. One can search for the presence of various files or check their contents. I personally add a human readable 'device.txt' file in the root directory which specifies what that particular machine is.

Instead of trying to work out what a machine is and what is present one can simply use try-except until you get an import which works -

Code: Select all

try    : import PySimpleGUI   as sg
except : import PySimpleGUI27 as sg

pfletch101
Posts: 634
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: How to distinguish that the python program is executed on a RPI?

Sat Oct 05, 2019 7:35 pm

hippy wrote:
Sat Oct 05, 2019 7:14 pm
Elmit wrote:
Fri Oct 04, 2019 11:50 pm
I am developing Python scripts on different machines, including RPI.

Is there a way to distinguish on which machine I am working, so that I load the right modules?
There is 'os.name' and I am sure others which can give you information about what the code is being run on. One can search for the presence of various files or check their contents. I personally add a human readable 'device.txt' file in the root directory which specifies what that particular machine is.

Instead of trying to work out what a machine is and what is present one can simply use try-except until you get an import which works -

Code: Select all

try    : import PySimpleGUI   as sg
except : import PySimpleGUI27 as sg
That will only give the desired results if the 'unwanted' modules are not available on the systems where they are not wanted. It won't deal with the situation where (for example) you may be choosing between modules that are available on some or all of the systems on which you don't want your script to load them. In that scenario, too, if the failure of the 'try' results from a real problem (other than intentional non-availability) with the relevant module, you may very well not want the alternative module to be silently loaded.

Elmit
Posts: 3
Joined: Thu Jul 25, 2019 5:51 am

Re: How to distinguish that the python program is executed on a RPI?

Mon Oct 07, 2019 1:43 am

Andyroo wrote:
Sat Oct 05, 2019 5:00 pm
You could look at /sys/firmware/devicetree/base/model to see what you are running on but how you would then dynamically select the include modules I have no idea...
that fails on Ubuntu. No such file!

hippy
Posts: 9173
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: How to distinguish that the python program is executed on a RPI?

Mon Oct 07, 2019 9:42 am

Elmit wrote:
Mon Oct 07, 2019 1:43 am
Andyroo wrote:
Sat Oct 05, 2019 5:00 pm
You could look at /sys/firmware/devicetree/base/model to see what you are running on but how you would then dynamically select the include modules I have no idea...
that fails on Ubuntu. No such file!
Which is therefore a perfect indicator that it's not running on something which does have that file.

There probably won't be a single thing which uniquely identifies every machine you may have. It will be a process of determining what there is and what there isn't - 'twenty questions' if you like - which will hopefully come up with the answer.

User avatar
RaTTuS
Posts: 10744
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: How to distinguish that the python program is executed on a RPI?

Mon Oct 07, 2019 10:12 am

look at
/etc/os-release along with
/proc/cpuinfo
/etc/issue
/etc/rpi-issue
and files you put there fir ID purposes
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

Return to “Beginners”