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
Add env var to set DEFAULT_PYTHON (add Pyenv support) #448
Conversation
0d378b9
to
801ddee
Compare
I am not a pyenv user (maybe one of the other pipx maintainers is?), so I don't really know how to test this or show that it fixes anything. Could you run some commands that show this fixes a problem a pyenv user would run into, and copy+paste them and their output here so I can see what that looks like? Assuming this change is useful and we want to merge, the help text will also need to document this environment variable (see |
Generally (for any user): the new env var should let you override the default python used by pipx to install packages. So, if you Specifically (for Pyenv users): this works for with So, this is testable without Pyenv simply by changing the env var and verifying that Pipx is using the correct Python to install packages. Pyenv TestsI tested this by running the latest pipx release side-by-side with this the source from this PR. The main thing to note is that the pipx release installed with python 3.8 both times, but the pipx source installed using the pyenv python (3.8 the first time, then 3.6 the second). https://asciinema.org/a/347120 $ pyenv global 3.8.3
$ python --version
Python 3.8.3
$ env | grep -i pipx_default_python
PIPX_DEFAULT_PYTHON=/Users/jonathan/.cache/pyenv/shims/python
$ python src/pipx install jrnl
installed package jrnl 2.4.3, Python 3.8.3
These apps are now globally available
- jrnl
done! ✨ 🌟 ✨
$ python src/pipx uninstall jrnl
uninstalled jrnl! ✨ 🌟 ✨
$ pipx install jrnl
installed package jrnl 2.4.3, Python 3.8.3
These apps are now globally available
- jrnl
done! ✨ 🌟 ✨
$ pipx uninstall jrnl
uninstalled jrnl! ✨ 🌟 ✨
$ pyenv global 3.6.10
$ python --version
Python 3.6.10
$ python src/pipx install jrnl
installed package jrnl 2.4.3, Python 3.6.10
These apps are now globally available
- jrnl
done! ✨ 🌟 ✨
$ python src/pipx uninstall jrnl
uninstalled jrnl! ✨ 🌟 ✨
$ pipx install jrnl
installed package jrnl 2.4.3, Python 3.8.3
These apps are now globally available
- jrnl
done! ✨ 🌟 ✨
$ pipx uninstall jrnl
uninstalled jrnl! ✨ 🌟 ✨
|
I needed to do something similar in #244 (comment). This would also be a good workaround for #395 as well. In general, |
Looks good to me, thanks for listing out that test case. That was helpful. I think we should add a unit test before landing. Since the tests on CI generally only have one python version installed, we can set |
77f3aec
to
22df9f3
Compare
I'm not super familiar with this project, so I'm not sure if I did what you asked, but I think this is ready now. |
It just occured to me, would it be worthwhile to incorporate |
tests/test_constants.py
Outdated
|
||
|
||
def test_default_python(monkeypatch): | ||
monkeypatch.setattr(constants, "DEFAULT_PYTHON", "bad_python") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be PIPX_DEFAULT_PYTHON
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure, but I don't think so. The way constants are set up, environment variables are read before the tests are run, so patching the environment variable itself (PIPX_DEFAULT_PYTHON
) wouldn't do anything here. So, we need to patch the python variable that holds the environment variable instead.
And I'm fairly new to your code base, so I might be wrong, but it seems to me this is the same way the other constants are handled in the test suite (like patching LOCAL_BIN_DIR
instead of PIPX_BIN_DIR
here).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could do it with importlib.reload(pipx.constants)
but I'm not sure if there would be side-effects.
Agreed. We should raise a |
Sounds good. I can add some better error output. For the use of |
I agree with this. I feel like being explicit in environment config is good. For our commands in an interactive shell it is nice that |
Contrary to instincts, a Alternatively, We can ban that by doing |
If we do use |
Hi, just checking back on this. I just need a final decision on whether or not to include |
I'm ok with it, and would like the logging.info() statement to show that we found |
The |
@itsayellow @uranusjr do you guys want to request changes or are you good merging like this? |
I suggest improving treatment to relative paths, either by using |
I suggest adding a
In general this will make debugging much easier in the future. |
Sounds good. I'll do the requested updates this week. Thanks for the feedback! |
@wren any progress on this, you still plan to do it? |
Ah, yes. Time got away from me in the last week, sorry! I'll make some time today for it. |
FYI I'm going to see if I can make a merge that will fix things soon. |
OK, if you merge pipx master now the tests should work again. |
6cd2530
to
d31b1e4
Compare
I just came back to check on this and realized I didn't hit submit on my comment. Sorry! -- So, I've made the requested changes. I also added a call to The only test failing is related to typing (since the variable can now be |
This keeps sys.executable as the default, but will use the PIPX_DEFAULT_PYTHON environment variable, if defined.
This avoids needing to update any type checking where DEFAULT_PYTHON is used.
d31b1e4
to
3ebc248
Compare
Hi, this is updated now. I believe this meets all of the previously established criteria:
Anything I've left out? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One minor nitpick, otherwise this looks good to me!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks mostly good to me, just one little nit.
Also one more thing: Could you please list PIPX_DEFAULT_PYTHON
and a help blurb for it in main.py
around line 50, inside PIPX_DESCRIPTION
, under "Optional Environment Variables".
Hey @wren , just in case it got lost, could you please list PIPX_DEFAULT_PYTHON and a help blurb for it in main.py around line 50, inside PIPX_DESCRIPTION, under "Optional Environment Variables"? Then I'll merge this. |
@itsayellow Yup, sorry. I'm in the middle of moving right now, so it'll be a few days before I can get to this. |
@itsayellow All ready for merging. Thanks for your patience! |
@wren, thanks for your persistence as well! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one more thing and I'll do it...adding changelog entry. |
Fixes #113.
This keeps sys.executable as the default, but will use the PIPX_DEFAULT_PYTHON environment variable, if defined. This mean no change in functionality for any users until they define the variable.
I'm suggesting the var
PIPX_DEFAULT_PYTHON
(noPYENV
even though the original issue asked for pyenv support), because this approach will work with any python manager, and I think it might be confusing to enshrine one manager in the var name over others.Also, it didn't look like the constants are tested, so I didn't update any tests. But let me know if I missed something and I'll be happy to add.