question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Making this work in virtual environments

See original GitHub issue

Various 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:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
lervagcommented, Jun 6, 2020

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.

0reactions
lervagcommented, Sep 12, 2020

Closing due to inactivity. Feel free to reopen if you (or anyone) can help settle/answer my questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Virtual Environments: A Primer
In this tutorial, you'll learn how to work with Python's venv module to create and manage separate virtual environments for your Python ...
Read more >
venv — Creation of virtual environments — Python 3.11.1 ...
The venv module supports creating lightweight “virtual environments”, each with their own independent set of Python packages installed in their site directories ...
Read more >
Virtual environments for absolute beginners — what is it and ...
Virtualenv is a Python package that allows us to create the venvs. We'll install it globally on our machine. Install virtualenv package using ......
Read more >
How to Set Up a Virtual Environment in Python – And Why It's ...
This tutorial will cover everything you need to know about virtual environments and how to set one up with Virtualenv.
Read more >
Working with Python virtual environments: the complete guide
There are two key parts to a virtual environment: ... Every virtual environment we create can be created with a different Python version....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found