Anki in Python package index?

agentydragon's Avatar

agentydragon

27 Mar, 2020 07:18 PM

Hi Damien (and others):

I have a bunch of code which I want to operate on the Anki database. It would be inconvenient to write this code as an Anki addon.
(It's here: https://github.com/agentydragon/anki-utils/blob/master/src/deploy/d.... It non-interactively updates card templates. I could implement it as an addon, where I'd have a dialog that would take a .zip or .json or whatever file and then set the card templates. I might actually end up using this solution if there's no other way, but let's for the sake of discussion leave that option aside.)

In my module I add where I know Anki's Python sources to be into sys.path and then import anki, and then do whatever I need on the anki module.

However, this recently broke for me when I switched from using Anki from Ubuntu's repositories to the official packages from https://apps.ankiweb.net/; while the apt package actually copied over Anki's .py files and ran them using system Python, Anki's official distribution (IIUC) has a bundled Python, the packages are binaries and I haven't figured out how to actually import them from my system Python (which is 3.7, not 3.8 as the one bundled with Anki). [there might be a way to do that but I haven't just figured it out yet myself]

It would be really practical for me if I could just say that my project has Anki's pylib as a PyPI dependency.
But the anki and ankirs packages in PyPI are just stubs saying to use GitHub to get the package.

Would it be feasible to publish the core (non-Qt) parts of Anki on PyPI?

(I know there is Rust code now, which is problematic, but I assume it's not impossible to solve - other Python packages also have native parts.)

More broadly speaking, this is a question of "how to correctly write and run a program that depends on Anki".
Right now, the only working process for running code that depends on Anki I can think of is:

  • git clone Anki somewhere on the disk
  • make it (so that the build information file is generated)
  • In your Python program, add a path in the Git repo into sys.path
  • import anki

This should work, but it's impractical, and I would hope there would be a better solution than this :(

I assume that it's technically possible to publish Anki's Python parts on PyPI, but it's probably not being done because it might take a lot of annoying setup.

Any ideas? Thoughts?

Thanks!

  1. Support Staff 1 Posted by Damien Elmes on 28 Mar, 2020 03:58 AM

    Damien Elmes's Avatar

    'make build' produces Python wheels you can install and redistribute to other machines. Evandro recently updated the CI infra so that they're also produced as part of the CI process, and they can be downloaded from the checks page.

  2. 2 Posted by agentydragon on 28 Mar, 2020 01:36 PM

    agentydragon's Avatar

    Neat! If I or someone else creates a process to also publish the wheels into PyPI as part of the release steps, would you be willing to merge the changes in?

  3. 3 Posted by addons_zz on 28 Mar, 2020 06:19 PM

    addons_zz's Avatar

    I could make it publish to pypi each new release (e.g., git tag 2.23) instead of each commit to master. By creating a GitHub secret https://github.com/ankitects/anki/settings/secrets on an environment variable with a token to pipy http://pyfound.blogspot.com/2019/07/pypi-now-supports-uploading-via...

  4. 4 Posted by agentydragon on 28 Mar, 2020 06:23 PM

    agentydragon's Avatar

    If Damien is OK with it and the setup works, it would be really helpful and I'd appreciate it :)

  5. Support Staff 5 Posted by Damien Elmes on 29 Mar, 2020 10:17 AM

    Damien Elmes's Avatar

    I can see how it could be useful - if you guys are happy to contribute the relevant scripts, I have no objections to having this run on tagged releases.

  6. 6 Posted by addons_zz on 30 Mar, 2020 04:29 AM

    addons_zz's Avatar

    I will be working on this. When the code is done, I will open a pull request: (https://github.com/ankitects/anki/pull/535 - Created the GitHub Actions step Upload to PyPi)

  7. Support Staff 7 Posted by Damien Elmes on 30 Mar, 2020 11:26 AM

    Damien Elmes's Avatar

    Thanks!

  8. 8 Posted by agentydragon on 05 Apr, 2020 08:39 AM

    agentydragon's Avatar

    Thanks for working on this!

    Damien, would you consider cutting a release soonish? I'm looking forward for having Anki in PyPI :)

  9. 9 Posted by addons_zz on 05 Apr, 2020 06:17 PM

    addons_zz's Avatar

    He could push the tag 2.24.1 on the same commit he pushed the tag 2.24

    https://semver.org/

  10. 10 Posted by agentydragon on 05 Apr, 2020 06:55 PM

    agentydragon's Avatar

    (I'd expect cutting a new release might be easier than applying the new PyPI push machinery to an old commit)

  11. 11 Posted by addons_zz on 05 Apr, 2020 07:15 PM

    addons_zz's Avatar

    After thinking, if he created a tag 2.24.1 over the old tag 2.24, it would probably not publish the package to pypi because on that time/commit, there was no such code on the GitHub checks.

    Then, he could just push the tag 2.24.1 over the latest commit on the master branch, as a "hotfix" for pypi.

  12. Support Staff 12 Posted by Damien Elmes on 06 Apr, 2020 04:07 AM

    Damien Elmes's Avatar

    I'm afraid it'll likely be at least a few more weeks before 2.1.24 is ready

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac