Module not found for python file in plugin directory

I am writing a plugin and I wish to keep certain functions in a separate file. It is called utils.py and it is in the same directory (beetsplug) that the main plugin file is in. I use from utils import * and I get the error module not found.

I am able to do this with a regular python program that also uses the utils.py file. For some reason, it does not work when importing from a beets plugin.

How can I import functions from the utils.py file in my plugin?

You’ll want to create a Python package (i.e., a directory with an __init__.py in it). Like the bpd plugin included with beets, for instance:

So, I just move all the functions into the __init__.py file under the directory named utils?

EDIT: OK, I think I figured it out. I moved everything into the __init__.py file inside utils folder inside beetsplug. Then, I put from beetsplug import utils

Now i do not get the error. I have not gotten far enough to know if I can access the functions yet.

Not that I’m an beets expert at all, but Python packages are generally named as the tool (here plugin) they represent. So, let’s say your plugin is called silence. Then you have two cases:

  1. If you all the stuff for your plugin is just in one file only, then call the file silence.py and place it directly in the plugin dir. (That’s the situation for most beets plugins.)

  2. If you have stuff in multiple files, create a directory with the name silence in the plugin dir and place all your files (here utils.py and your main file as __init__.py) under your directory silence.

    In your main file you then can import the utils module as

     from beetsplug.silence import utils
    

Hope this helps!

1 Like