Python 3 in Embedded Script

Posted on
Sun Apr 18, 2021 9:03 am
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Python 3 in Embedded Script

Is there a way to use Python 3 using embedded script or file ?

I've tried this without success in Indigo. It runs on my local machine and runs on PyCharm on the server

Code: Select all
#!/usr/bin/env python3.8
from shapely.geometry import Point, Polygon

coords = [
(39.008234937234256, -77.37684452531161),
(39.00809324520176, -77.37627518191934),
(39.00733602026089, -77.37671994280922),
(39.0076547326009, -77.37724949835487)
]
homeFence = Polygon(coords)

lat = 39.00777
long = -77.37668600000001
bmw = Point(lat, long)

if bmw.within(homeFence):
    #update indigo variable with True
    print(True)
else:
    print(False)


Posted on
Sun Apr 18, 2021 9:41 am
DaveL17 offline
User avatar
Posts: 6751
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python 3 in Embedded Script

ryanbuckner wrote:
Is there a way to use Python 3 using embedded script or file ?

Not as such. But you can use Python 2.7 to run a script in a subprocess that calls Python 3x.

This Stack Overflow post has an answer that shows how to do this. You can get your True/False with the STDOUT pipe (you'd RETURN the bool instead of PRINT it).

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Apr 18, 2021 9:44 am
FlyingDiver offline
User avatar
Posts: 7210
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python 3 in Embedded Script

Hmm. Try running it as a shell script instead of an embedded python script. If you tell it to put the output in a variable, you should be able to use that variable in other logic.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Apr 18, 2021 10:20 am
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Python 3 in Embedded Script

Ok, I think I get the gist. A little more help and I'll be there. I think I'm missing how to return the value back to the main process from the subprocess. p.stout = <_io.FileIO name=3 mode='rb' closefd=True> when I print it.

1) I have the embedded script which does this:

Code: Select all
import sys
import indigo
from subprocess import Popen, PIPE

p = Popen(['python3', 'bmwgeo.py'], stdout=PIPE, bufsize=0)
print(p.stdout)

indigo.variable.updateValue(339870612,value=p.stdout)


2) then I have a file called bmwgeo.py in my Python2-Includes directory on the server that does this:
Code: Select all
#!/usr/bin/env python3.8
from shapely.geometry import Point, Polygon


def isPointInside():
    coords = [
    (39.008234937234256, -77.37684452531161),
    (39.00809324520176, -77.37627518191934),
    (39.00733602026089, -77.37671994280922),
    (39.0076547326009, -77.37724949835487)
    ]
    homeFence = Polygon(coords)

    lat = 39.00777
    long = -77.37668600000001
    bmw = Point(lat, long)

    return bmw.within(homeFence) #returns True or False


isPointInside()


Posted on
Sun Apr 18, 2021 10:44 am
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Python 3 in Embedded Script

FlyingDiver wrote:
Hmm. Try running it as a shell script instead of an embedded python script. If you tell it to put the output in a variable, you should be able to use that variable in other logic.


Ok Im going to go this direction. RETURN is not outputting the results from the python file. How do I return the value from the python script to the shell ?

Posted on
Sun Apr 18, 2021 10:54 am
FlyingDiver offline
User avatar
Posts: 7210
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python 3 in Embedded Script

I think it's standard out, so
Code: Select all
print("{}".format(bmw.within(homeFence)), file = sys.stdout)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Apr 18, 2021 11:13 am
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Python 3 in Embedded Script

FlyingDiver wrote:
I think it's standard out, so
Code: Select all
print("{}".format(bmw.within(homeFence)), file = sys.stdout)


appreciate all your help on this. The .py file works properly and prints True / False to standard out.

Does the Indigo Shell receive this stdout when running the .sh file? Below is my file and Indigo throws an error. When I run the BMWgeo.sh file from terminal is returns True

Code: Select all
#!/bin/sh
python3 bmwgeo.py


Event Log
Code: Select all
Apr 18, 2021 at 1:01:35 PM
   Action Group                    Geofence BMW
   Action Collection Error         Run Shell Script action: script /Library/Application Support/Perceptive Automation/Python2-includes/bmwgeo.sh exited abnormally with a return code of: 2

Posted on
Sun Apr 18, 2021 11:18 am
FlyingDiver offline
User avatar
Posts: 7210
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python 3 in Embedded Script

Run the bmwgeo.py directly, no shell script. You shouldn't need the shell script, if the shebang line is correct, and the python file has executable permissions.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Apr 18, 2021 11:25 am
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Python 3 in Embedded Script

FlyingDiver wrote:
Run the bmwgeo.py directly, no shell script. You shouldn't need the shell script, if the shebang line is correct, and the python file has executable permissions.


SUCCESS!!! I was over complicating it of course. You're the best.

Posted on
Sun Apr 18, 2021 12:01 pm
DaveL17 offline
User avatar
Posts: 6751
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python 3 in Embedded Script

Great!

I learned something here, too. Didn't realize that Indigo could run an external script in Python 3x directly. I'm so used to working through plugins that I was overcomplicating it too.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Apr 18, 2021 7:22 pm
DaveL17 offline
User avatar
Posts: 6751
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python 3 in Embedded Script

I thought it might be helpful to add a full example in case the discussion above wasn't clear. Create the Python 3 script file and save it to a location that Indigo has access to. Here's an example script that will fail in Python 2.7 but will work in this context.

foo.py
Code: Select all
#! /usr/bin/env python3
# -*- coding: utf-8 -*-

some_list = [1, 2, 3]
first, *rest = some_list
print(rest)
In Indigo,
- run foo.py as a "Run Shell Script" action.
- if desired, store the result in an Indigo variable (the value sent by the 'print' command above will be redirected as output to the variable.) The above example will set the designated variable value to "[2, 3]".

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon Apr 19, 2021 6:57 am
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Python 3 in Embedded Script

DaveL17 wrote:
I thought it might be helpful to add a full example in case the discussion above wasn't clear. Create the Python 3 script file and save it to a location that Indigo has access to. Here's an example script that will fail in Python 2.7 but will work in this context.

foo.py
Code: Select all
#! /usr/bin/env python3
# -*- coding: utf-8 -*-

some_list = [1, 2, 3]
first, *rest = some_list
print(rest)
In Indigo,
- run foo.py as a "Run Shell Script" action.
- if desired, store the result in an Indigo variable (the value sent by the 'print' command above will be redirected as output to the variable.) The above example will set the designated variable value to "[2, 3]".



One more thing I had to do that wasn’t obvious. The directions using shell paths in Indigo say to use backslash as escape characters for spaces. This errored in the Indigo interface. Using quotes around the path worked.

Posted on
Mon Apr 19, 2021 7:30 am
DaveL17 offline
User avatar
Posts: 6751
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python 3 in Embedded Script

ryanbuckner wrote:
One more thing I had to do that wasn’t obvious. The directions using shell paths in Indigo say to use backslash as escape characters for spaces. This errored in the Indigo interface. Using quotes around the path worked.

+1

Excellent point.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Wed Apr 21, 2021 4:01 pm
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Python 3 in Embedded Script

One thing I haven't figured out how to do is access Indigo through the Python3 script.

My best workaround is to have Python2.7 script save a variable value to a file, then have the Python3 script pick it up and use it.

I wasn't able to import indigo using [sudo] pip3 import indigo ... can I do it manually?

Posted on
Wed Apr 21, 2021 4:14 pm
FlyingDiver offline
User avatar
Posts: 7210
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python 3 in Embedded Script

ryanbuckner wrote:
One thing I haven't figured out how to do is access Indigo through the Python3 script.

My best workaround is to have Python2.7 script save a variable value to a file, then have the Python3 script pick it up and use it.

I wasn't able to import indigo using [sudo] pip3 import indigo ... can I do it manually?


No, you can't do anything like that. On my plugins where I have a Python3 sub-process, I pass configuration data as command line arguments.

Unfortunately, it does not appear that you can include arguments with the Indigo action. So you might need to use a small embedded Python script to kick off the Python 3 script.

Something like:

Code: Select all
Popen(["/usr/bin/python3", '/path/to/py3script.py', 'Arg1','Arg2'])


Then add this to the start of your Python3 script:

Code: Select all
import sys
arg1 = sys.arvg[1]
arg2 = sys.argv[2]

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Who is online

Users browsing this forum: No registered users and 4 guests

cron