Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import error when running from a venv #238

Open
jbrubake opened this issue Mar 1, 2023 · 2 comments
Open

Import error when running from a venv #238

jbrubake opened this issue Mar 1, 2023 · 2 comments

Comments

@jbrubake
Copy link

jbrubake commented Mar 1, 2023

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
Copy link

maxwell-k commented Jul 26, 2023

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 <module>
      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 pushed a commit to maxwell-k/peru that referenced this issue Jul 26, 2023
The curl plugin for peru uses the following as its shebang line:

    #! /usr/bin/env python3

If the user is using pipx, then the system Python interpreter —
`/usr/bin/env python3` —  likely does not have the peru package
installed.

Before this change the curl plugin always uses a `get_version` function
from the peru package and therefore will error if the Python interpreter
does not have the peru package installed.

After this change the curl plugin can succeed with only standard library
modules available.
maxwell-k added a commit to maxwell-k/peru that referenced this issue Jul 26, 2023
The curl plugin for peru uses the following as its shebang line:

    #! /usr/bin/env python3

If the user is using pipx, then the system Python interpreter —
`/usr/bin/env python3` —  likely does not have the peru package
installed.

Before this change the curl plugin always uses a `get_version` function
from the peru package and therefore will error if the Python interpreter
does not have the peru package installed.

After this change the curl plugin can succeed with only standard library
modules available.
@maxwell-k
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants