xxyzz / WordDumb

A calibre plugin that generates Kindle Word Wise and X-Ray files for KFX, AZW3, MOBI and EPUB eBook.
https://xxyzz.github.io/WordDumb/
GNU General Public License v3.0
386 stars 19 forks source link

Issue with Python Detection for pyenv Installations #135

Closed ghost closed 1 year ago

ghost commented 1 year ago

Hello,

I've encountered an issue where the plugin only uses the homebrew_mac_bin_path function to find the path for Python installed via Homebrew. For users like me who have installed Python through pyenv, the path cannot be correctly found.

As a temporary workaround, I modified the homebrew_mac_bin_path function as follows:

import shutil
...
def homebrew_mac_bin_path(package: str) -> str:
    # stupid macOS loses PATH when calibre is not launched in terminal
    if shutil.which(package):
        return shutil.which(package)
    elif platform.machine() == "arm64":
        return f"/opt/homebrew/bin/{package}"
    else:
        return f"/usr/local/bin/{package}"

With these changes, running calibre-customize -b . && calibre-debug -g works correctly.

Here are the details of my system:

I suggest that the plugin's Python detection mechanism should be improved to cater for Python installations via different methods, not limited to Homebrew.

Thank you for your attention to this matter.

xxyzz commented 1 year ago

You probably didn't notice the comment in the homebrew_mac_bin_path function, shutil.which() returns None when you click calibre icon normally from Launchpad because PATH will be empty. I not sure if pyenv would work in that condition.

ghost commented 1 year ago

Following our previous discussion regarding the OutdatedPython exception when launching Calibre via the GUI, I've made further investigations and want to share my findings.

Just as you pointed out, shutil.which() was indeed returning None because the PATH was empty when launching Calibre from the Launchpad. I understand that this was the reason the homebrew_mac_bin_path function was unable to find the Python executable installed via pyenv.

However, I discovered that I could bypass this limitation by manually adding the Python path to the environment variables used by Calibre's GUI mode. This can be done by adding the following line to ~/Library/Preferences/calibre/macos-env.txt (as suggested in the Calibre's documentation):

PATH=/Users/yourusername/.pyenv/versions/3.9.1/bin:$PATH

After making this change, the plugin was able to successfully locate the correct Python version and function as expected when launching Calibre from the GUI.

I would like to clarify that this solution worked for me personally, and while it may not be universally applicable, I believe it might serve as a feasible reference or starting point for users who, like me, are using Python installed via pyenv and launching Calibre via the GUI. I hope this information will be helpful to you and others who might encounter the same issue.

xxyzz commented 1 year ago

https://github.com/xxyzz/WordDumb/commit/9e9f6d33420953f5d8ec19823bc209b5725aab21 uses shutil.which() if can't find the binary file in Homebrew's directory. Thanks for your investigation and I have also update the plugin's install doc.

And Python 3.9 is not supported, please use Python 3.10 or newer version. Because calibre's embedded Python is 3.10.

ghost commented 1 year ago

The python version was just used as an example, thanks for the heads up