Build fails: python interpreter not inside a virtualenv?

Hi, I’m trying to build Anki from source but I keep running into this error:

maturin failed
Caused by: Expected `python` to be a python interpreter inside a virtualenv ಠ_ಠ

The python interpreter specified in my path is inside a virtual environment I’ve created using venv. I’ve tried different environments and Python installations but the result is the same (or another error, even earlier in the building process).
Does anybody have an idea what could be causing this? What special environment set-up is Anki expecting?
I would be grateful for any input.

I can’t seem to reproduce the issue with Cygwin and the latest master. Maybe try Git for Windows instead of Cygwin.

C:\>uname -s
MSYS_NT-10.0-19041

Evandro reported and fixed it in Makefile a while ago and I’m not sure why it’d appear again.

  1. Only now I had run for the first the the make build command (on Windows), and I figure out it was not working due this problem (PyO3/maturin#283 - Expected python to be a python interpreter inside a virtualenv). Then, to fix it I had to manually edit the anki/pyenv/Scripts/activate script. Also, I am not sure why, but the Python interpreter on the GitHub Actions Windows was create the file anki/pyenv/Scripts/activate with CRLF line endings, which I had to convert to LF, other the shell script would never be sourced.

    ...
    "${PYTHON_BIN}" -m venv pyenv && \
    case "$$(uname -s)" in CYGWIN*|MINGW*|MSYS*) \
        dos2unix "${ACTIVATE_SCRIPT}" && \
        sed -i -- "s@VIRTUAL_ENV=\".*\"@VIRTUAL_ENV=\"$$(pwd)/pyenv\"@g" "${ACTIVATE_SCRIPT}"; \
        ;; esac && \
    . "${ACTIVATE_SCRIPT}" && \
    ...
    
3 Likes

I was mainly trying MinGW and cmd, I had also given Cygwin a shot.
But what can I say… After reading your answer I tried Cygwin one more time and it worked! I must have done something else wrong when I last tried it.
Still the same error message with other consoles, though.
My bash skills are close to non-existent, but if someone else wants to investigate the issue, I can give further information.

For now, I’m just happy that I can finally build Anki. Thanks a lot, @kelciour. :grinning:

I’m getting this error too, on Ubuntu 20.04, as well after setting up a virtual environment with venv

I’ve just been spending the last few hours on this, with no luck :frowning_face:
I tried using venv with ‘env’, also with ‘pyenv’ as I see the scripts using. Now I tried with pyenv and I still get the same error (maturin failed Caused by: Expected python to be a python interpreter inside a virtualenv ಠ_ಠ).

Any ideas?

I haven’t looked into this in super great detail, but here is my impression: because of the way that Anki’s build system bootstraps its own virtual environment, trying to build Anki with an activated virtual environment seems to be problematic. Activating a virtual environment before building Anki with ./run or make build can cause problems; I think this is because it will then try to create a virtual environment within a virtual environment.

I suggest running make clean and perhaps even rm -r pyenv to remove the virtual environment created by Anki’s build scripts, then deactivate your virtual environment or start a new shell session. As long as you have the required python version installed and in your PATH (re: README.development), you should be able to build Anki with ./run. This might seem scary, but Anki will bootstrap its own python virtual environment and will not pollute your global python environment, so no worries there.

4 Likes

As I wrote above, the trick for me was to run make run -B in Cygwin. But that would also only work if I had deleted the pyenv folder before (after a failed build) or I would still get the error.

1 Like

Thank you for your reply, Andrew. I tried that, too, but I don’t know how to get it to work, because Ubuntu doesn’t have an executable called python, only python3. I tried to create a function and export it:

python() {
	python3 "$@"
}
export -f python

That does work correctly, because if I type python I get to the python3 interactive shell (which doesn’t happen without this function), but when running run or make run I just get back to the initial error, Expected python to be a python interpreter inside a virtualenv ಠ_ಠ).

At this point I’m a bit at a loss how to move forward…

And if I delete the pyenv folder it first downloads pip, and then setuptools, then pyqt5, then pyqtwebengine, then PyQt5-sip, and then fails with /bin/bash: maturin: command not found.

If now I try to:

source pyenv/bin/activate
pip install maturin
deactivate

then it finally works! Gosh, that was complicated :woozy_face:

So it seems that maturin isn’t correctly mentioned as a dependency? Also, it sounds important to mention in the build instructions to not use a virtual env for pythong doing it, and to tell Ubuntu users how to overcome the lack of a python executable.

That isn’t the case, which you can verify by taking a look here and here

I think that is a good point. Maybe you can submit a PR with those additions? I think a simple note mentioning that Anki configures the virtual env for you would be helpful!

While not covered in detail, it is mentioned in README.development that one must have Python 3.7+ installed. That being said, it is a bit confusing since /usr/bin/python isn’t available on 20.04. I didn’t know that! :exploding_head: Check this out:

Remaining packages in Ubuntu which require Python 2.7 have been updated to use /usr/bin/python2 as their interpreter, and /usr/bin/python is not present by default on any new installs. On systems upgraded from previous releases, /usr/bin/python will continue to point to python2 for compatibility. Users who require /usr/bin/python for compatibility on newly-installed systems are encouraged to install the python-is-python3 package, for a /usr/bin/python pointing to python3 instead.

I would strongly advise against this method of configuring Python. While I definitely commend your persistent efforts :crossed_swords:, that is not a sustainable solution. I would try following the instructions from the Ubuntu wiki I linked to above.

On Ubuntu 18.04, I had to follow instructions similar to those here.

For example:

update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
update-alternatives --set python3 /usr/bin/python3.7

and some symlink magic due to a weird issue described here:

ln -s /usr/lib/python3/dist-packages/apt_pkg.cpython-36m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/apt_pkg.cpython-37m-x86_64-linux-gnu.so

I don’t think it’s worth debugging this at this point, since it was in the context of not having python correctly configured.

1 Like

I’ve mentioned that a venv is not required in the docs on Changing Anki's build system to Bazel

1 Like

Firstly, sorry for the late reply.

I’m getting a 404 for the first link, and in the second link a search for maturin only finds the comment #https://github.com/PyO3/maturin/issues/283 - Expected python to be a python interpreter inside a virtualenv. Sorry if I am missing something obvious. I imagine it’s been updated since you wrote the post?

Many thanks for the reference, it’s super useful to know about the python-is-python3 package! I imagine, though, that others in my situation might also not know about it?

I suggest you try the latest beta, as Anki has moved away from maturin since your original post.