-
-
Notifications
You must be signed in to change notification settings - Fork 151
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
Respect the active virtual environment instead of the installed location #362
Comments
Thanks for the feature request!
Assuming I understand you correctly, I'm not sure if it's possible to have the #!/usr/local/bin/python
# -*- coding: utf-8 -*-
import re
import sys
from pipdeptree.__main__ import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main()) Notice the shebang line. This would mean we would need to instead programmatically detect that the user has activated a virtual environment. I did think of one approach while looking at the venv docs. When the virtual environment is activated, it would set the environment variable $ source venv/bin/activate
(venv) $ echo $VIRTUAL_ENV
/workspaces/pipdeptree/venv From here we could snatch the path to the environment's interpreter and use it to grab it's associated packages. The only problem with this is I'm not sure yet if the poetry, conda, or others who fire up environments will set $VIRTUAL_ENV. Open to other approaches. |
What if the shebang was replaced by #!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import subprocess
if __name__ == '__main__':
sys.exit(subprocess.Popen(['which', 'python'])) Calling this echos the python path of the activated environment or default to the system if none is activated. |
Not cross platform or supporting pypy. |
Also this would need to be an option flag or breaking change and major version bump. |
This sadly won't work as one problem I can see with this would be that the virtual environment will need to have |
I'm confused. I thought
Isn't that the current situation if |
That only works on Unix not on Windows. |
I think you may have meant if |
No, I meant is not. The idea was that, if When |
I see, you are saying that without specifying the --python option users would have to have Currently, I do not see any other approach to this besides looking for environment variables, i.e. $VIRTUAL_ENV. I would need to investigate other projects (e.g. poetry, conda) that implement virtual environments and see if they have this env var or something similar. If this works out, I think one way of avoiding a breaking change or adding an option flag is by passing something like: pipdeptree --python auto-detect # or --python auto Here we would run a routine trying to determine what virtual environment implementation they are using by searching for certain environment variables. If all else fails, warn the user that we couldn't detect any virtual environment and fallback to using the system python. We could also just consider this a failure and exit. As I mentioned, this approach depends on whether the other implementations do set certain env vars that map to paths we could use to get their interpreter. And another thing, if these implementations make changes to this in the future, we would need to account for this (possibly also consider different versions of the implementation). |
I like the environment variable approach. Seems more reliable. I think |
After further thought I do think this is worthwhile to pursue. I don't expect these virtual environment implementations to frequently change the way that we can snatch the env's prefix as users actively rely on them (i.e. this shouldn't be a maintenance burden). I'll tackle this when I get the time. |
You should be good to go after I release 2.21.0! I'm kind of iffy about introducing an environment variable, mainly because --python auto doesn't fallback on the system environment if we can't find a virtual environment (so you would have to unset that env var if you want to output the global packages) . Feel free to open a new issue about it if you want to discuss it or if you encounter any bugs with the current implementation. |
Works great. Thank you for the new feature! |
Yes it does make sense to print an info message in this case; I'll open up a PR for it when I can. |
Describe the feature
If
pipdeptree
is installed in the system python location (to make it always accessible), it will by default use the system python to look for packages. When a virtual environment is activated, this causes a confusing situation where packages listed do not correspond to the activated environment.To work around this, a user has to manually provide the desired python:
Installing
pipdeptree
as a system-wide utility seems relevant to avoid reinstalling it each time for each environment (similar to whatpoetry
recommends).It seems to me that
pipdeptree
should prioritize the "active" python rather than its installed location.The text was updated successfully, but these errors were encountered: