# Help with Libraries/Shared for Sharing Code

**URL:** <https://forums.plex.tv/t/help-with-libraries-shared-for-sharing-code/55759>\
**Category:** Dev/API Corner\
**Tags:** plugin-dev\
**Created:** [January 29, 2014, 12:11am UTC](https://forums.plex.tv/t/help-with-libraries-shared-for-sharing-code/55759 "2014-01-29T00:11:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![beothuk](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@beothuk](https://forums.plex.tv/u/beothuk)\
**Post date:** [January 29, 2014, 12:11am UTC](https://forums.plex.tv/t/help-with-libraries-shared-for-sharing-code/55759/1 "2014-01-29T00:11:01Z")

</div>

I have Plex Plugin, that works, ([https://github.com/stuckless/sagetv-phoenix-plex-channel](https://github.com/stuckless/sagetv-phoenix-plex-channel)), and it contains a main Channel (\_\_init\_\_.py), a Search Service and a URL Service. &nbsp;&nbsp;

&nbsp;

I need to share code between all 3 parts, and I've googled, and I've search, and I found that you can use a "Libraries/Shared" folder to put in shared code (although I saw in one plugin that it used a "Shared Code" folder... so I'm not 100% sure which is correct).

&nbsp;

My problem is that the Libraries/Shared seems fairly useless for actually sharing code between the different parts of my channel. &nbsp;I created a very simple use case ...&nbsp;

&nbsp;

In my Shared/Libraries/ I have a file sagedebug.py

&nbsp;

```
def debugit(val='x'):
    Log("xxxxxxxxxxxxxxxxxxxx:" + val)
```

and if I call it from my init.py...

&nbsp;

```
import sagedebug as xdebug
...
def Start():
    ...
    xdebug.debugit('init.py')
```

I get an error....

> 2014-01-28 18:51:27,735 (-48dcf700) : &nbsp;CRITICAL (sandbox:306) - Exception when calling function 'Start' (most recent call last):
> 
> &nbsp; File "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/code/sandbox.py", line 297, in call\_named\_function
> 
> &nbsp; &nbsp; result = f(\*args, \*\*kwargs)
> 
> &nbsp; File "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-ins/SageTVPhoenix.bundle/Contents/Code/\_\_init\_\_.py", line 82, in Start
> 
> &nbsp; &nbsp; xdebug.debugit('init.py')
> 
> &nbsp; File "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-ins/SageTVPhoenix.bundle/Contents/Libraries/Shared/sagedebug.py", line 4, in debugit
> 
> &nbsp; &nbsp; Log("xxxxxxxxxxxxxxxxxxxx:" + val)
> 
> NameError: global name 'Log' is not defined
> 
> &nbsp;
> 
> &nbsp;

&nbsp;

&nbsp;

So, it would appear that I can't use the Plex Logging facilities from the shared file??

&nbsp;

In another case, I replaced the Log statement with a simple print statement to log out the value of a Prefs[] field... and it didn't work :(

```
print("DEBUG: " + Prefs['server'] )
```

Again, I get an error that Prefs is not valid.

&nbsp;

So, it would appear that I share code code, like a string utility, but I can't use the Plex system in the shared code, and I can't access any Plex Plugin preferences for my channel?? &nbsp;I'm pretty sure I must be doing something wrong, since this is a pretty trival use case.

&nbsp;

Currently, to get around this, I have common file, sagex.py, that I actually have to cut and paste into each of my ServiceCode.pys files :( &nbsp;(This is not a very maintainable way to organize "shared" code)

&nbsp;

I'd appreciate if anyone could tell me how to properly have a shared code section.

&nbsp;

Thanks,

&nbsp;

&nbsp;

&nbsp;

&nbsp;

---

<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:** [January 29, 2014, 7:16pm UTC](https://forums.plex.tv/t/help-with-libraries-shared-for-sharing-code/55759/2 "2014-01-29T19:16:35Z")

</div>

If you want your Services to have access to the shared code, then it needs to be located in a directory called "Shared Code" within the plugin's Services directory. It will also need to be named xxx.pys rather than xxx.py (still a python file).

Take a look at the [NHL Channel](https://github.com/mikedm139/NHL.bundle/blob/master/Contents/Services/Shared%20Code/gamecenter.pys) for an example.

This method has the added benefit of allowing you to use Plex framework functions such as Log().

Handling the import is different in the channel's \_\_init\_\_.py than it is in the ServiceCode.pys.

In the [NHL channel's \_\_init\_\_.py](https://github.com/mikedm139/NHL.bundle/blob/master/Contents/Code/ __init__.py#L5), you can see that I defined a function as a reference to the shared code via the SharedCodeService..

In the URL Service, it can be done with a more straightforward [import statement](https://github.com/mikedm139/NHL.bundle/blob/master/Contents/Services/URL/NHL/ServiceCode.pys#L1).

The other method (/Libraries/Shared/file.py) is more intended for external libraries. It's not used much so I'm having trouble thinking of examples. The plugin framework treats it entirely as a separate code library and as such it has no access to the plugin framework API. It is also not available for use by URL Services. In general, I would recommend against this method if you can avoid it.

---

<div class="post-metadata">

**Author:** ![beothuk](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@beothuk](https://forums.plex.tv/u/beothuk)\
**Post date:** [January 29, 2014, 7:46pm UTC](https://forums.plex.tv/t/help-with-libraries-shared-for-sharing-code/55759/3 "2014-01-29T19:46:17Z")

</div>

Thank you very much... I KNEW I had to be missing something. &nbsp;I can't wait to try this in a few hours :)

Thanks!

---

<div class="post-metadata">

**Author:** ![JeppeTh](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@JeppeTh](https://forums.plex.tv/u/JeppeTh)\
**Post date:** [May 9, 2014, 11:48am UTC](https://forums.plex.tv/t/help-with-libraries-shared-for-sharing-code/55759/4 "2014-05-09T11:48:38Z")

</div>

I finally managed to import a "webserver" I made - but it doesn't work really - responses aren't getting through.

I guess there's some limitation on what to run in Plex?

File descriptors are hijacked or something?

This isn't getting through nor cause an exception

```
    self.send_response(200) 
    self.send_header('Content-Type', 'text/xml')
    self.end_headers()
    with open('data.file') as f:
        data = f.read()
        f.close()
        self.wfile.write(data)
```

---

<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 21, 2019, 1:10am UTC](https://forums.plex.tv/t/help-with-libraries-shared-for-sharing-code/55759/5 "2019-12-21T01:10:37Z")

</div>

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