buildinspace / peru

a generic package manager, for including other people's code in your projects
MIT License
1.12k stars 69 forks source link

Import error when running from a venv #238

Open jbrubake opened 1 year ago

jbrubake commented 1 year ago

I used pipx to install peru in a venv. peru runs the git module properly but the curl module fails. Specifically the import peru.main at line 13 in peru/resources/plugins/curl/curl_plugin.py fails

To test, I installed peru using pip but not inside a venv and it works fine so its either something weird that pipx is doing, or the venv in general

maxwell-k commented 1 year ago

I am new to peru and a long time user of pipx. peru looks really promising and pipx is brilliant. It would be super to get them working together.

Reproducing the issue Start with a cut down version of the example in the README: imports: pathogen: .vim/autoload/ curl module pathogen: url: https://codeload.github.com/tpope/vim-pathogen/tar.gz/v2.3 Make sure `~/.local/bin` is on the current PATH Install `pipx` mkdir --parents ~/.local/bin \ && curl -Lo ~/.local/bin/pipx https://github.com/pypa/pipx/releases/download/1.2.0/pipx.pyz \ && chmod u+x ~/.local/bin/pipx Install peru using pipx: pipx install peru Make sure git is installed Try to sync: peru sync The error message is: In target "pathogen": Traceback (most recent call last): File "/home/maxwell-k/.local/pipx/venvs/peru/lib64/python3.11/site-packages/peru/resources/plugins/curl/curl_plugin.py", line 13, in import peru.main ModuleNotFoundError: No module named 'peru' I reproduced this on Fedora 38, so Python 3.11.

I believe this issue relates to the plugin architecture for peru. IIUC peru plugins are executable files; for this issue curl_plugin.py is the relevant plugin.

If I install peru with pipx; then pipx sets up a virtual environment and installs peru there. pipx adds a symbolic link to the peru command from that virtual envinronment to the PATH. Continuing the example above, the shebang line is:

#!/home/maxwell-k/.local/pipx/venvs/peru/bin/python

However the shebang line for curl_plugin.py is:

#! /usr/bin/env python3

This means that curl_plugin.py is executed by a Python interpreter that does not have the peru package installed. That explains the error message!

@oconnor663 is support for pipx something you would consider?

Thank you for your work on peru!

If anyone has suggestions for an implementation perhaps I can start on a PR. I'm not very familiar with the peru code; one option might be to switch to an entry point for curl_plugin.py so that pipx will update the shebang line or to special case .py files.

Workaround As a workaround is to manually change the shebang line for `curl_plugin.py`, for example: cd ~/.local/pipx/venvs/peru/lib/python3.11/site-packages/peru/resources/plugins/curl \ && mv curl_plugin.py curl_plugin.py.bak \ && head --lines=1 ~/.local/bin/peru > curl_plugin.py \ && tail --lines=+2 curl_plugin.py.bak >> curl_plugin.py \ && chmod u+x curl_plugin.py This change will be lost on any subsequent upgrade. It also makes installation of peru with pipx more complex. Support for installation with pipx would be ideal. I appreciate that this is open source and maintainers and contributors are volunteers. Thank you for reading!
maxwell-k commented 1 year ago

Turns out there was a much easier fix than I expected; I've opened a pull request.

To test the proposed fix using the steps to reproduce above; in place of using pipx install peru to install peru, try:

pipx install git+https://github.com/maxwell-k/peru

Or if you prefer to run sync directly:

pipx run --spec=git+https://github.com/maxwell-k/peru peru sync