Making this work in virtual environments
See original GitHub issueVarious issues (#15 , #13 ) were talking about how to make this work in a virtual environment like pipx.
I’ve found a hacky workaround for this, but it involves editing the apy
source code yourself. I’ve done this with poetry so far and it seemed to work, I’ll try it with pipx later.
Basically, wherever you have apy
installed, you need to find the cli.py
file and edit the top part so it looks like this instead:
"""A script to interact with the Anki database"""
import os
import click
# ultra-hacky way of getting this to work in a virtual environment
import sys, subprocess
command = "/usr/bin/python -c 'import sys; print(sys.path)'"
new_path = eval(subprocess.check_output(command, shell=True).decode("utf-8"))
# this means it will look on the system python path for imports first,
# then the virtual environment's original path
sys.path = new_path + sys.path
# the aqt_data_folder() function in aqt.utils depends on sys.prefix to
# find what it's looking for; sys.prefix depends on the interpreter used
# to launch the script, which means importing aqt will always fail in a
# virtual environment unless we override sys.prefix temporarily.
old_prefix = sys.prefix
sys.prefix = "/usr"
from apy.anki import Anki
from apy.config import cfg, cfg_file
sys.prefix = old_prefix
# rest of file continues here
You may need to edit the /usr/bin/python
part to whatever is the path of the interpreter that your Anki install is using, and the sys.prefix = /usr
part to whatever is two directories up from your aqt_data
folder (i.e. whatever corresponds to my /usr/share/aqt_data/
on your system).
It works:
[ckp95@ckp95-desktop apy-test]$ poetry run apy --help
reverting to stock json
Usage: apy [OPTIONS] COMMAND [ARGS]...
A script to interact with the Anki database.
The base directory may be specified with the -b / --base option. For
convenience, it may also be specified in the config file
`~/.config/apy/apy.json` or with the environment variable APY_BASE or
ANKI_BASE. This should point to the base directory where Anki stores it's
database and related files. See the Anki documentation for information
about where this is located on different systems
(https://apps.ankiweb.net/docs/manual.html#file-locations).
A few sub commands will open an editor for input. Vim is used by default.
The input is parsed when one saves and quits. To abort, one should exit
the editor with a non-zero exit code. In Vim, one can do this with the
`:cquit` command.
One may specify a different editor with the EDITOR environment variable.
For example, to use emacs one can add this to ones `~/.bashrc` (or
similar) file:
export EDITOR=emacs
Note: Use `apy subcmd --help` to get detailed help for a given subcommand.
Options:
-b, --base TEXT Set Anki base directory
-h, --help Show this message and exit.
Commands:
add Add notes interactively from terminal.
add-from-file Add notes from Markdown file.
check-media Check media
info Print some basic statistics.
list List cards that match a given query.
model Interact with Anki models.
review Review marked notes.
sync Synchronize collection with AnkiWeb.
tag List tags or add/remove tags from matching notes.
I’ll leave this open in case anyone has a better way of doing this. Needless to say it’s very hacky and could easily get broken by an update to either Anki or apy, so it’s just a stopgap.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (6 by maintainers)
Top GitHub Comments
Ah, I realize it is harder than I imaged, and that we might need a hack such as you suggest. That is, it seems we can’t just modify the path itself, we also need to change the prefix when importing anki. Not sure how to do that in a “reliable” manner.
For now, I’ll leave this open and unsolved. I’ll be happy to accept a well thought out solution for this, though!
In the meantime, I’ll rather suggest that one installs this systemwide and not inside a virtualenv, as that should work in most cases, I think.
Closing due to inactivity. Feel free to reopen if you (or anyone) can help settle/answer my questions.