# Cannot share code between \_\_init\_\_.py and search service

**URL:** <https://forums.plex.tv/t/cannot-share-code-between-__init__-py-and-search-service/95386>\
**Category:** Dev/API Corner\
**Tags:** plugin-dev\
**Created:** [March 1, 2015, 2:32pm UTC](https://forums.plex.tv/t/cannot-share-code-between-__init__-py-and-search-service/95386 "2015-03-01T14:32:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![schaumburg01](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@schaumburg01](https://forums.plex.tv/u/schaumburg01)\
**Post date:** [March 1, 2015, 2:32pm UTC](https://forums.plex.tv/t/cannot-share-code-between-__init__-py-and-search-service/95386/1 "2015-03-01T14:32:00Z")

</div>

I have written a plug-in (MythRecordings - see elsewhere on this forum) with an \_\_init\_\_.py file that works fine. Now I want to add a search service - and I've got a lot of code I want to share between the two.

&nbsp;

But I cannot figure that out - and I suspect it's because I've structured my directories wrong.

&nbsp;

Here's my best shot at a directory tree - the channel (\_\_init\_\_.py) runs, it uses the SharedCodeService to access the shared code (plexmythlib.pys), which can get hold of the shared resource (somedata.json):&nbsp;

```
+ MythRecordings.bundle
	+ Contents
		  Info.plist
		+ Code
			__init__.py
		+ Services
			  ServiceInfo.plist
			+ Search
				+ MythRecordings
					ServiceCode.pys
			+ Shared Code
				plexmythlib.pys
			+ Resources
				somedata.json
```

I've registered my services as folllows in ServiceInfo.plist:

```
<?xml version="1.0" encoding="UTF-8"?>

	Search
	
		MythRecordings
		
			Identifier
			dk.schaumburg-it.plexapp.mythrecordings
```

But I cannot get Plex to recognize my search service - looking at the error message, it seems to expect to find that code under

&nbsp;

MythRecordings.bundle/Contents/Search Services/MythRecordings/ServiceCode.pys

&nbsp;

instead of

&nbsp;

MythRecordings.bundle/Contents/Services/Search/MythRecordings/ServiceCode.pys

&nbsp;

But if I rename Services/Search =\> Search Services, my \_\_init\_\_.py cannot find the shared code anymore. And if I just move the ServiceCode.pys to the expected location, and leave the shared code where it is, the ServiceCode.pys cannot find the shared code in plexmythlib.pys.

&nbsp;

Whenever I move things around, one thing starts working, and another stops.

&nbsp;

Gaaarh - I'm confused! 

&nbsp;

**Edit:**

&nbsp;

Specifically, if I try to load the shared code as follows (from the ServiceCode.pys):

```
GetMythTVRecordings = SharedCodeService.plexmythlib.GetMythTVRecordings

def Search(query):  
oc = ObjectContainer(title1=‘Myth Search service results – Contents/Services/Search/MythRecordings/ServiceCode.pys’, title2=‘Search Results’)  
return oc
```

I get

&nbsp;

FrameworkException: ('No shared code object named "%s" was found.', 'plexmythlib')

---

<div class="post-metadata">

**Author:** ![manfer](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/manfer/32/10663_2.png) [@manfer](https://forums.plex.tv/u/manfer)\
**Post date:** [March 4, 2015, 12:36pm UTC](https://forums.plex.tv/t/cannot-share-code-between-__init__-py-and-search-service/95386/2 "2015-03-04T12:36:05Z")

</div>

Read the answer to the following thread

[https://forums.plex.tv/topic/96745-help-with-librariesshared-for-sharing-code/](https://forums.plex.tv/topic/96745-help-with-librariesshared-for-sharing-code/)

It is explained how and with a link to a channel code that actually does it.

In \_\_init\_\_.py it is used the way you did:

```
GetMythTVRecordings = SharedCodeService.plexmythlib.GetMythTVRecordings
```

And in ServiceCode.pys&nbsp;you will have to import it this way:

```
from plexmythlib import GetMythTVRecordings
```

---

<div class="post-metadata">

**Author:** ![schaumburg01](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@schaumburg01](https://forums.plex.tv/u/schaumburg01)\
**Post date:** [March 5, 2015, 10:44am UTC](https://forums.plex.tv/t/cannot-share-code-between-__init__-py-and-search-service/95386/3 "2015-03-05T10:44:33Z")

</div>

@manfer

Thanks for helping out - however, I had followed the directions in that exact post, which was what made the whole thing so perplexing!

However - it turned out that there was an error in the shared code. It was clearly stated in the log, about half a page up, but I just fixated on the last "No shared code object named "%s" was found"&nbsp;line.

My apologies for wasting everyones time here&nbsp; :(

For those having trouble with shared code, the conclusion is: the directory structure I outlined actually does work, as does the ServiceInfo.plist

---

<div class="post-metadata">

**Author:** ![Gerk](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/gerk/32/81980_2.png) [@Gerk](https://forums.plex.tv/u/Gerk)\
**Post date:** [March 6, 2015, 1:31am UTC](https://forums.plex.tv/t/cannot-share-code-between-__init__-py-and-search-service/95386/4 "2015-03-06T01:31:57Z")

</div>

Glad you got it worked out. &nbsp;Sometimes finding things in the various logs can get interesting for sure ... that sort of stuff has tripped me up many times.

---

<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:** [January 8, 2020, 7:47pm UTC](https://forums.plex.tv/t/cannot-share-code-between-__init__-py-and-search-service/95386/5 "2020-01-08T19:47:32Z")

</div>


