Where to put Python functions/classes on disk

Posted on
Mon Oct 14, 2019 2:33 pm
dstrickler offline
User avatar
Posts: 340
Joined: Oct 08, 2010
Location: Boston, MA

Where to put Python functions/classes on disk

I'm just now (I know, late) switching my many AppleScript files into Python. I've got a small script as an experiment, but I can't seem to add it as part of a function library as I could with AppleScript.

In AppleScript, I would create the AppleScript file, and place it in the Attachments folder.
In Python, I've been told to do the same, but use the directory "/Library/Python/2.7/site-packages" instead. The reload your libraries & attachments from the Indigo Plugins menu.

Now when I code a python script in Indigo and reference this python function, it doesn't find the function and errors out. How do I make sure this function is available to all my python code?

Thanks,

Dave

Posted on
Mon Oct 14, 2019 4:39 pm
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Where to put Python functions/classes on disk

dstrickler wrote:
Now when I code a python script in Indigo and reference this python function, it doesn't find the function and errors out. How do I make sure this function is available to all my python code?

It's tough to know how to help when we don't know what the error is that you're seeing. I created a file in my /Library/Python/2.7/site-packages folder called foo123.py:
Code: Select all
#!/usr/bin/env python
# -*- coding: utf-8 -*-

def bar():
    return "hello world."
Then I called it from an embedded script in Indigo using:
Code: Select all
import foo123

x = foo123.bar()

indigo.server.log(x)
And it runs without error:
Code: Select all
   Script                          hello world.

Please post back with the actual errors that you're seeing.

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

[My Plugins] - [My Forums]

Posted on
Mon Oct 14, 2019 5:11 pm
dstrickler offline
User avatar
Posts: 340
Joined: Oct 08, 2010
Location: Boston, MA

Re: Where to put Python functions/classes on disk

Yeah, that would help, huh? :o
Sorry - here are the supporting scripts running under v10.7.5 of OSX

I placed this code in /Library/Python/2.7/site-packages in a file called "dws.py".
Code: Select all
#!/usr/bin/env python
# -*- coding: utf-8 -*-

try:
    import indigo
except ImportError:
    print "Attachments can only be used from within Indigo"
    raise ImportError

import datetime
from datetime import timedelta
import time

def minutesSince(indigoTimestamp, indigoVariable):
   indigoTimestamp = indigo.variables[indigoTimestamp]
   date_time_obj = datetime.datetime.strptime(indigoTimestamp, '%A, %B %d, %Y %H:%M:%S %p')
   timestamp_now = datetime.datetime.now()
   delta_minutes = (timestamp_now - date_time_obj).seconds / 60
   indigo.server.log("Updating var " + indigoVariable + " with " + str(delta_minutes))
   indigo.variable.updateValue(indigoVariable, value=unicode(delta_minutes))


Then I am running this code inside of an Indigo Action. I've verified it's loading the code, because when I change "dws.py" to "xyz_dws.ply" is throws an error saying it can't find the file (as it should). I've also verified I'm running v2.7 of Python.
Code: Select all
import dws

import sys
indigo.server.log("Running Python: " + sys.version)

minutesSince("motionWholeHouseTimestamp", "minutesSinceMotionWholeHouse")


Code: Select all
   Script                          Running Python: 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)]
   Script Error                    embedded script: global name 'minutesSince' is not defined
   Script Error                    Exception Traceback (most recent call shown last):

     embedded script, line 9, at top level
NameError: global name 'minutesSince' is not defined

Dave

Posted on
Mon Oct 14, 2019 5:27 pm
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Where to put Python functions/classes on disk

try:
Code: Select all
dws.minutesSince("motionWholeHouseTimestamp", "minutesSinceMotionWholeHouse")

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

[My Plugins] - [My Forums]

Posted on
Mon Oct 14, 2019 5:35 pm
dstrickler offline
User avatar
Posts: 340
Joined: Oct 08, 2010
Location: Boston, MA

Re: Where to put Python functions/classes on disk

Doh! That fixed it!

Thanks very much for your help - this is just the leg up I needed.

Dave

Posted on
Mon Oct 14, 2019 5:44 pm
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Where to put Python functions/classes on disk

Good deal. Good luck with your project.

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

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 5 guests

cron