Python script works through interactive terminal only

Posted on
Tue Dec 10, 2019 11:36 am
DPattee offline
Posts: 453
Joined: Jan 14, 2004
Location: Redmond, WA

Python script works through interactive terminal only

I worked out a script using the interactive indigo python terminal which uses a couple of imports that I'd already installed via pip install. (Python 2.7.16, from Indigo 7.3.1)

Pasting the script in to the embedded python box of a Schedule window, clicking 'run', just gives an error:
Code: Select all
Dec 10, 2019 at 9:34:45 AM
   Script Error                    embedded script: No module named tzlocal
   Script Error                    Exception Traceback (most recent call shown last):

     embedded script, line 2, at top level
ImportError: No module named tzlocal


That initial portion of the script works fine throuhg the terminal though:
Code: Select all
Python 2.7.16 (default, Oct 16 2019, 00:35:27)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]
Connected to Indigo Server v7.3.1, api v2.1 (localhost:1176)
>>> from datetime import datetime
>>> import tzlocal
>>> from tzlocal import get_localzone
>>> local_tz = tzlocal.get_localzone()
>>> import pytz
>>>
>>> print local_tz
America/Los_Angeles
>>>



The datetime and pytz parts work, it just chokes on the tzlocal import, which I need because some plugins' "lastupdated" times are just written in local time with no indicator, so I need to be able to apply a timezone and then convert them to UTC for other purposes.

Posted on
Tue Dec 10, 2019 2:28 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python script works through interactive terminal only

What's the exact box that you're trying to run from (Schedules themselves don't have Python script boxes)?

Also, in a normal terminal window paste in:

Code: Select all
which python


And let me know what you get.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Dec 10, 2019 6:45 pm
DPattee offline
Posts: 453
Joined: Jan 14, 2004
Location: Redmond, WA

Re: Python script works through interactive terminal only

Not sure what else to call it... The create new schedule -> actions -> server actions/script and file actions/execute script -> embedded python box, where I paste the script in and click the 'run' button above the text box, which puts the error message in both the event log and in the schedule window, right below the python script box.

From the terminal on that machine:
Code: Select all
which python
/usr/local/bin/python


And pip install put the tzlocal package in the same place that the pytz package and elasticsearch packages went, both of which are accessible from indigo's interactive and the schedule area
Code: Select all
/usr/local/lib/python2.7/site-packages

Posted on
Tue Dec 10, 2019 7:31 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python script works through interactive terminal only

DPattee wrote:
From the terminal on that machine:
Code: Select all
which python
/usr/local/bin/python



Which is the wrong location for Python. On my system:

Code: Select all
 % which python
/usr/bin/python


Which means you have a non-standard Python install. Which is why you're having problems.

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

Posted on
Tue Dec 10, 2019 7:34 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python script works through interactive terminal only

Code: Select all
% pip show pytz
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Name: pytz
Version: 2013.7
Summary: World timezone definitions, modern and historical
Home-page: http://pythonhosted.org/pytz
Author: Stuart Bishop
Author-email: stuart@stuartbishop.net
License: MIT
Location: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
Requires:
Required-by: twilio, pandas, Django

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

Posted on
Tue Dec 10, 2019 7:50 pm
DPattee offline
Posts: 453
Joined: Jan 14, 2004
Location: Redmond, WA

Re: Python script works through interactive terminal only

Yes, that's the standard location for homebrew installed python 2 that is kept more up to date than the OS one.

pip install for the pytz, elasticsearch, and tzlocal packages all went in to the same place (usr/local/...), with the elasticsearch and tzlocal packages being accessible through the indigo interactive python cli, but only the elasticsearch package being accessible through the in-app one. I'd expect the IndigoPluginHost -i to behave identically to the in-app execution...

Posted on
Tue Dec 10, 2019 7:58 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python script works through interactive terminal only

DPattee wrote:
Yes, that's the standard location for homebrew installed python 2 that is kept more up to date than the OS one.

pip install for the pytz, elasticsearch, and tzlocal packages all went in to the same place (usr/local/...), with the elasticsearch and tzlocal packages being accessible through the indigo interactive python cli, but only the elasticsearch package being accessible through the in-app one. I'd expect the IndigoPluginHost -i to behave identically to the in-app execution...


It's well established that using any non-standard Python install causes Indigo to act strangely.

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

Posted on
Tue Dec 10, 2019 8:00 pm
DPattee offline
Posts: 453
Joined: Jan 14, 2004
Location: Redmond, WA

Re: Python script works through interactive terminal only

Here's the output showing the interactive and in-app versions running different versions of python, despite being run as the same user account so theoretically with the same path, etc:

Script of:
Code: Select all
import sys
for p in sys.path:
  indigo.server.log(p)


typed in to the indigopluginhost interactive cli:

Code: Select all
/Library/Application Support/Perceptive Automation/Indigo 7.3/IndigoPluginHost.app/Contents/Resources/PlugIns/InteractiveShell.indigoPlugin/Contents/Server Plugin
/Applications/PyCharm.app/Contents/debug-eggs/pydevd-pycharm.egg
/Applications/PyCharm.app/Contents/debug-eggs/pycharm-debug.egg
/Library/Application Support/Perceptive Automation/Indigo 7.3/IndigoPluginHost.app/Contents/Resources/PlugIns
/Library/Application Support/Perceptive Automation/Indigo 7.3/Scripts/Attachments
/usr/local/Cellar/python@2/2.7.17/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/usr/local/Cellar/python@2/2.7.17/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/usr/local/Cellar/python@2/2.7.17/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/usr/local/Cellar/python@2/2.7.17/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/usr/local/Cellar/python@2/2.7.17/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/usr/local/Cellar/python@2/2.7.17/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/usr/local/Cellar/python@2/2.7.17/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/usr/local/Cellar/python@2/2.7.17/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/site-packages


vs being typed in to an embedded python text box in the app's schedule editor and clicking 'run' (using indigo.server.log instead of print of course):
Code: Select all
Dec 10, 2019 at 5:57:18 PM
   Script                          /Library/Application Support/Perceptive Automation/Indigo 7.3/IndigoPluginHost.app/Contents/Resources/PlugIns/ScriptExecutor.indigoPlugin/Contents/Server Plugin
   Script                          /Applications/PyCharm.app/Contents/debug-eggs/pydevd-pycharm.egg
   Script                          /Applications/PyCharm.app/Contents/debug-eggs/pycharm-debug.egg
   Script                          /Library/Application Support/Perceptive Automation/Indigo 7.3/IndigoPluginHost.app/Contents/Resources/PlugIns
   Script                          /Library/Application Support/Perceptive Automation/Indigo 7.3/Scripts/Attachments
   Script                          /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg
   Script                          /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
   Script                          /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
   Script                          /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
   Script                          /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
   Script                          /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
   Script                          /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
   Script                          /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
   Script                          /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
   Script                          /Library/Python/2.7/site-packages
   Script                          /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
   Script                          /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC

Posted on
Tue Dec 10, 2019 8:04 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python script works through interactive terminal only

Like I said, having multiple installs of Python breaks Indigo. That's just the way it is.

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

Posted on
Tue Dec 10, 2019 8:06 pm
DPattee offline
Posts: 453
Joined: Jan 14, 2004
Location: Redmond, WA

Re: Python script works through interactive terminal only

So how do you install a module in a way that Indigo will successfully use? Because I did the same method for elasticsearch as I did for tzlocal, both ended up in the same place, but only one is visible...

Posted on
Tue Dec 10, 2019 8:07 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python script works through interactive terminal only

DPattee wrote:
So how do you install a module in a way that Indigo will successfully use? Because I did the same method for elasticsearch as I did for tzlocal, both ended up in the same place, but only one is visible...


Delete the home-brew version. Then the only pip you have will be the one that installs where the standard version expects to find it.

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

Posted on
Tue Dec 10, 2019 9:32 pm
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: Python script works through interactive terminal only

While the root problem is the non-standard python, it is strange that the interactive shell is behaving differently than an embedded script. I'd expect them to be the same. Let me see if I can figure out why that is occurring.

Okay, after glancing at this I'm pretty sure the InteractiveConsole module Indigo uses for the interactive shell is including the shell path variables such that the non-standard python is getting used instead (just at it would be from Terminal).

Image

Posted on
Tue Dec 10, 2019 10:41 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python script works through interactive terminal only


Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri May 15, 2020 2:11 pm
calamo7 offline
Posts: 4
Joined: Feb 10, 2016

Re: Python script works through interactive terminal only

Hi everyone,
I've been having the same problem with a module loading in the interactive shell and not from an action in the server. My Mac is running Catalina, Indigo 7.4 with a standard Python 2.7 and "sudo easy_install pip", which I think is pretty much the standard procedure to get 2.7 going. I'm a new to Python so it took me a while to find out the problem but haven't been able to find a permanent solution see below.

I ran the same code provided by Dpatee below in the shell and within a script, and found out that in the interactive shell you also get your "User/(user)/Library/Python/2.7/lib/python/site-packages" which is where I found my packages were going, and explaining the reason why my packages could get loaded in the shell but not by the server.

Code: Select all
import sys
for p in sys.path:
  indigo.server.log(p)


I've been trying to make pip to install the packages in my root "/Library/Python/2.7/lib/python/site-packages" folder instead, but the only solution I found to work was to use the pip option -t '/Library/Python/2.7/lib/python/site-packages' and force pip to install in the right folder for Indigo to use it. It works but is not permanent.

Any help in pointing me out on how to "fix" pip to install the packages by default would be appreciated I've been trying many options without any luck. Also, probably there is a way to add the user packages path to the configuration, but maybe then we run into permission problems.

Posted on
Fri May 15, 2020 2:27 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python script works through interactive terminal only

Run pip using "sudo":
Code: Select all
sudo pip install XXXX

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

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 6 guests