# Implement Callbacks in Different Module than \_\_init\_\_.py

**URL:** <https://forums.plex.tv/t/implement-callbacks-in-different-module-than-__init__-py/13177>\
**Category:** Dev/API Corner\
**Tags:** plugin-dev\
**Created:** [December 11, 2011, 4:48pm UTC](https://forums.plex.tv/t/implement-callbacks-in-different-module-than-__init__-py/13177 "2011-12-11T16:48:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![till213](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/till213/32/2814_2.png) [@till213](https://forums.plex.tv/u/till213)\
**Post date:** [December 11, 2011, 4:48pm UTC](https://forums.plex.tv/t/implement-callbacks-in-different-module-than-__init__-py/13177/1 "2011-12-11T16:48:15Z")

</div>

Function named 'morePhotos' couldn't be found in the curre
Hello,  
  
  
How does Plex "resolve" its Callback functions?  
  
  
I want to implement parts of my Instagram plugin logic in a different module "InstaStream.py". That source file is located in the same location as my \_\_init\_\_.py source file.  
  
In \*\*InstaStream.py\*\* I have:  
  

```auto

<br />
def readStream(title, url):<br />
    oc = ObjectContainer(title2 = title, view_group = 'Pictures')<br />
    ...<br />
    oc.add(DirectoryObject(key = Callback(morePhotos, url=nextUrl, title=title), title = 'Next...'))<br />
    return oc<br />

```

  
  
In that same file I have the Callback:  
  

```auto

<br />
def morePhotos(title, url):<br />
    return readStream(title = title, url = url)<br />

```

  
  
However when I run that code my callback is not called and I get in the plugin Log:  
  
\*\*"Function named 'morePhotos' couldn't be found in the current environment"\*\*  
  
When I place the callback morePhotos \*\*into my \_\_init\_\_.py\*\* file, \*\*then it works\*\* (and by changing the function call to "return InstaStream.readStream(...)").  
  
Now this introduces a mutual dependency on these modules. I can live with that, but want to understand what's going on and whether I'm doing something wrong here (mind you, I'm still new to Plex Plugin development as well as Python).  
  
Off course I could also place everything into \_\_init\_\_.py - but you know, old programmer's habbit of structuring my code ;)  
  
  
So how does this "Callback" (aka "Function" in pre-V2 Plugin API) work? I read somewhere in this forum that Plex imposes some restrictions onto "external Python modules". Is this one of them? Or does Plex simply assume that the "Callback" has to be implemented in \_\_init\_\_.py? Or am I simply not understanding the concept of "function callbacks" in Python? (Currently I have "function pointers" in mind, which probably won't help much here ;))  
  
Thanks, Oliver

---

<div class="post-metadata">

**Author:** ![mikedm139](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/mikedm139/32/96909_2.png) [@mikedm139](https://forums.plex.tv/u/mikedm139)\
**Post date:** [December 11, 2011, 7:20pm UTC](https://forums.plex.tv/t/implement-callbacks-in-different-module-than-__init__-py/13177/2 "2011-12-11T19:20:36Z")

</div>

I think you should be able to call those functions if you import the necessary file.

```auto

 import InstaStream 

```

  
I can't really comment on whether your approach is "right" or "wrong" from the Plex framework perspective but, I believe it \*should\* work.

---

<div class="post-metadata">

**Author:** ![till213](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/till213/32/2814_2.png) [@till213](https://forums.plex.tv/u/till213)\
**Post date:** [December 11, 2011, 8:35pm UTC](https://forums.plex.tv/t/implement-callbacks-in-different-module-than-__init__-py/13177/3 "2011-12-11T20:35:12Z")

</div>

> [@](#):
>
> I think you should be able to call those functions if you import the necessary file.  
> 
> ```auto
> 
> import InstaStream 
> 
> ```

  
  
Yes, I do have such an import statement in my \_\_init\_\_.py module (otherwise I wouldn't have been able to call the functions therein in the first place).  
  
However the situation is even worse: I have to have some "dummy" (empty) functions with the expected function signature in the InstaStream.py source file, otherwise I get an error:  
  
NameError: global name 'morePhotos' is not defined  
  
So apparently the "Callback" function expects a "global name" in the same source file (InstaStream.py in this case), but when the "lookup" is actually done the implementation in \_\_init.py\_\_ is called.  
  
  
So I have:  
  

```auto

<br />
# InstaStream.py:<br />
def someFunction(...)<br />
    oc = ObjectContainer(...)<br />
    ...<br />
    oc.add(DirectoryObject(key = Callback(morePhotos, url=nextUrl, title=title), ...) <br />
<br />
<br />
def morePhotos(url, title):<br />
    # dummy implementation, never called<br />
    return None<br />

```

  
  
And:  
  

```auto

<br />
# __init__.py<br />
import InstaStream<br />
<br />
...<br />
def morePhotos(url, title):<br />
    # This implementation is actually called<br />
    return InstaStream.readStream(url = url, title = title)<br />

```

  
  
  
Does that make sense?

---

<div class="post-metadata">

**Author:** ![mikedm139](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/mikedm139/32/96909_2.png) [@mikedm139](https://forums.plex.tv/u/mikedm139)\
**Post date:** [December 12, 2011, 3:41am UTC](https://forums.plex.tv/t/implement-callbacks-in-different-module-than-__init__-py/13177/4 "2011-12-12T03:41:51Z")

</div>

Hmmm… That is strange. The simplest thing to do would probably be to just contain all the code in the **init**.py file. However, there is a flag you can add to the info.plist file which might help.

```auto

<br />
<key>PlexPluginCodePolicy</key><br />
<string>Elevated</string><br />

```

  
I know it's required to be able to use 3rd party libraries and it might help here as well.

---

<div class="post-metadata">

**Author:** ![till213](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/till213/32/2814_2.png) [@till213](https://forums.plex.tv/u/till213)\
**Post date:** [December 12, 2011, 10:59am UTC](https://forums.plex.tv/t/implement-callbacks-in-different-module-than-__init__-py/13177/5 "2011-12-12T10:59:27Z")

</div>

> [@](#):
>
> The simplest thing to do would probably be to just contain all the code in the \_\_init\_\_.py file.

  
  
Yes, that would work. However I would really like to "group" my code into different modules, if possible. And as I am still new to Plex API and Python as well, I am just trying to understand currently how this "Callback" mechanism works.  
  
So I assume the Plex API "Callback" function takes a "function variable", creates some "string" out of it (a "function signature") and when the callback is actually to take place then the framework tries to "lookup that function name in the \_\_init.py\_\_ module/namespace" - probably by simply "assembling" a function call at runtime like "\_\_init\_\_" + callback, resulting in something like "\_\_init\_\_.morePhotos", and when that fails (Python exception of some sort) the said error "Function not found in context" is produced.  
  
Maybe if I would also add an explicit:  
  
import morePhotos from InstaStream  
  
in my \_\_init\_\_.py, maybe that would make a difference?  
  

> [@](#):
>
> However, there is a flag you can add to the info.plist file which might help.   
> 
> ```auto
> 
> <br />
> <key>PlexPluginCodePolicy</key><br />
> <string>Elevated</string><br />
> 
> ```

  
  
Yes, thank you, that was the flag I stumbled across already somewhere in this forum :). I'll give that one a try as well and let you know.  
  
  
Thanks, Oliver

---

<div class="post-metadata">

**Author:** ![till213](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/till213/32/2814_2.png) [@till213](https://forums.plex.tv/u/till213)\
**Post date:** [December 12, 2011, 11:44am UTC](https://forums.plex.tv/t/implement-callbacks-in-different-module-than-__init__-py/13177/6 "2011-12-12T11:44:21Z")

</div>

> [@](#):
>
> Yes, thank you, that was the flag I stumbled across already somewhere in this forum :)

  
  
For the record: http://forums.plexapp.com/index.php/topic/18324-updating-plugins-to-v2-framework/  
  
Cheers, Oliver

---

<div class="post-metadata">

**Author:** ![mikedm139](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/mikedm139/32/96909_2.png) [@mikedm139](https://forums.plex.tv/u/mikedm139)\
**Post date:** [December 12, 2011, 12:38pm UTC](https://forums.plex.tv/t/implement-callbacks-in-different-module-than-__init__-py/13177/7 "2011-12-12T12:38:03Z")

</div>

I wish I could be more help. I think someone else might be better able to answer your questions. I think I’m out of my depth at this point.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [December 20, 2019, 9:30pm UTC](https://forums.plex.tv/t/implement-callbacks-in-different-module-than-__init__-py/13177/8 "2019-12-20T21:30:43Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
