-
Notifications
You must be signed in to change notification settings - Fork 91
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
Wrong package versions within virtualenv #15
Comments
Thanks for the info; it's definitely worth looking into it! It works just fine for me via conda, so maybe just a few questions before we dig deeper into this issue ... ;)
I am definitely open to improvements if this is indeed a "bug" in this case. Do you have any recommendations? Ideally something that doesn't require external dependencies? |
Thanks for the quick response!
I ran some tests in fresh environments both with and with jupyter installed in the environment. The test fails without jupyter installed (as reported) but passes when jupyter is installed. So installing jupyter within the environment fixes the issue. Maybe that's the right thing to do anyway? I haven't ever done it before, though. It's probably worth noting this as a requirement in the watermark documentation. Or maybe adding jupyter as a requirement that is installed along side watermark? An aside on the ipython console: I don't know if people use watermark from the ipython console, but if so, simply installing ipython in the virtualenv may not work if the system ipython has been run previously within the virtualenv. Here is an example:
|
Hm, I usually install ipython/jupyter notebook as well in virtual environments, but maybe that's not a "normal" thing to do ;). Just to summarize your points to make sure that I understand it correctly:
But you are saying that if you run the system's jupyter notebook in a virtual environment, it will import the virtual environment's packages, not the system python packages, right? So, I guess what we want watermark to report the package version of the package that the jupyter notebook actually imports, no matter whether it's a system or virtual environment notebook. How to do this elegantly I don't know, yet. Hopefully, there's a way to achieve this via |
Right.
This is the behavior I would expect. I will give it some thought and let you know if I have any bright ideas. |
Unfortunately, I don't have a solution at hand (yet), but I just checked in via conda (running the non-virtual environment jupyter notebook in an active virtual conda environment, and it imports the "correct" package, that is, the package from the active virtual environment. So my guess is it's somehow an issue that is related to |
Reporting the same package info that is available via So that would add pip as a dependency (which seems reasonable), but then
|
Yeah that could work. Plus I think that an additional flag Have you checked if this grabs the correct numpy version in your system jupyter nb, i.e., the virtualenv one? Let me know if you'd like to submit it as a pull request, otherwise, I am happy to give it at try on the weekend. I think it would also be worthwhile posting this issue on the virtualenv issue tracker -- I think this is likely not desired behavior (e.g., since |
Yes, this grabs the correct version for me when I use the system jupyter notebook. I'll submit a PR later this evening. |
Alternatively, we could use something like import sys
module_names = ['numpy']
for m in module_names:
if hasattr(sys.modules[m], '__version__'):
print(m, sys.modules[m].__version__) I just see that pip is also using
I would really prefer a solution without adding additional dependencies here if possible |
The sys.modules solution works for me. I'll submit a new PR dropping the pip requirement. I also have a separate issue (must be 2.7 specific?) using the -w flag. |
Thanks for the PR!
Yeah, I think that would be a leaner solution! I would extend it a bit to cover some edge cases that don't have a import sys
module_names = ['numpy']
for m in module_names:
s += '%s ' % m
if hasattr(sys.modules[m], '__version__'):
s += sys.modules[m].__version__
elif hasattr(sys.modules[m], 'version'):
s += sys.modules[m].version
else:
s += 'n/a'
s = s.rstrip() |
I was just suggesting something like that regarding the version :-) How do you feel about throwing a custom exception if |
Yeah, great idea! btw. don't feel rushed with implementing everything, I am probably not able to take a close look at the PR before the weekend! |
Oooops, my sys.modules proposal was a bit naive as I just figured out after the pull request. It worked fine first, but then it didn't. So, this method currently only works if the module has been imported _before_ calling watermark. E.g., if watermark is the first cell in a notebook, it won't find the packages. I guess we need to fix that again ... :( Let's not rush it though and think about some better options (and if nothing else works, just using pip instead) PS: Reminds me that I want to improve the CI at some point #14 |
Sorry, didn't mean to rush. I just had time last night and thought I'd take the opportunity to try to take care of it. Here is an admittedly hacky solution but it works:
This calls the virtualenv python, so it returns the correct package info. I tried combining the for loop into a one-liner call to subprocess but for whatever reason couldn't get it to work, e.g.
|
No worries! Instead of using subprocess, wouldn't a simpler
EDIT: Alternatively,
would work as well.But I am not sure if there's an advantage vs
What do you think about something like: packages = ['numpy']
for p in packages:
imported = __import__(p)
# will raise an ImportError: No module named p
# so we could remove the PackageNotFoundError(Exception)
# I guess
try:
ver = imported.__version__
except AttributeError:
try:
ver = imported.version
except AttributeError:
try:
ver = imported.version_info
except AttributeError:
ver = 'n\a'
print(ver) I think this could work if I am not overlooking something :) |
Just had some extra time and it is (hopefully) fixed now (#19)! Thanks for the discussion and help, without your help, I probably wouldn't have discovered this tricky version bug! |
I'm not sure how widespread this issue is, or if it's something particular to my setup, but when I use watermark to report package information (using -p) within a jupyter notebook that is running in a virtualenv, version information about system level packages are reported rather than packages installed within my environment. If a package is installed in my virtualenv and not installed at the system level, then I end up getting
DistributionNotFound
exceptions when I try to report the version number using watermark.The problem stems from the call to
pkg_resources.get_distribution
. If I useget_distribution
directly from within my notebook to report the version of e.g.numpy
it shows my system level numpy info (v 1.11.0) rather than info about numpy installed within my virtualenv (v 1.11.1). For example:Similarly
But when I check the version of what is imported:
If I use
pkg_resources
from a python console or script running within a virtualenv, it reports the proper information about packages. But if I use it from an ipython console or jupyter notebook it reports system level information.If this is a widespread issue, should
watermark
find another more robust solution to getting package information other than usingpkg_resources
?The text was updated successfully, but these errors were encountered: