0

How do I ship some standard modules from Python together with my code?

I'm writing an add-on for Anki, for which I need Queue and threading modules from Python2.7 standard library.

When I try launching Anki, I get ImportError: No module named Queue. I assume that is because Anki does not ship with full Python interpreter and if I am missing any standard modules, I am to bundle them myself.

From Anki docs on addons:

Standard Modules

Anki ships with only the standard modules necessary to run the program - a full copy of Python is not included. For that reason, if you need to use a standard module that is not included with Anki, you’ll need to bundle it with your add-on.

So my question is: what steps do I take to bundle standard Python modules threading and Queue together with my add-on?

Note that add-ons in Anki are just Python scripts that have certain extra modules available.

julka
  • 1,172
  • 2
  • 13
  • 29

2 Answers2

3

From the Anki doc:

For a simple one-file add-on, you can upload the .py file. For multi-file add-ons, please create a subfolder that acts as a Python package, and create a small .py file that imports that package. Using the Japanese support add-on as an example, the structure looks like:

japanese/file1.py
japanese/file2.py
japanese/__init__.py # can be empty; marks the folder as a package
japanese/<binary support files>
jp.py

To upload a multi-file add-on, please zip up the folder and the loader .py file and upload the zip.

The <binary support files> can be the modules you want.

Checkout html_cleaner and image-occlusion-enhanced on Github if you want to see how others do it.

Community
  • 1
  • 1
0

For anybody else who is wondering how to import a .so file (I was using a library that tried to import parser and discovered that parser.__file__ was a .so file) the answer is it's the same as a .py file:

Create a directory (mkdir parser), copy the .so file into that directory (cp parser.cpython-37m-x86_64-linux-gnu.so parser/) and then add an __init__.py to the directory (touch parser/__init__.py).

This is almost certainly not cross platform, but it worked for my needs.