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

AttributeError: module 'psutil_linux' has no attribute '__file__' #934

Closed
thijstriemstra opened this issue Oct 31, 2016 · 4 comments
Closed

Comments

@thijstriemstra
Copy link
Contributor

thijstriemstra commented Oct 31, 2016

#564 adds detection for version mismatches, like the code below.

I've modified this code to print out some debug info:

# Sanity check in case the user messed up with psutil installation
# or did something weird with sys.path. In this case we might end
# up importing a python module using a C extension module which
# was compiled for a different version of psutil.
# We want to prevent that by failing sooner rather than later.
# See: https://github.com/giampaolo/psutil/issues/564
if (int(__version__.replace('.', '')) !=
        getattr(_psplatform.cext, 'version', None)):
    print(__version__)
    print(int(__version__.replace('.', '')))
    print(getattr(_psplatform.cext, 'version', None))
    print(_psplatform.cext.__file__)
    msg = "version conflict: %r C extension module was built for another " \
          "version of psutil (different than %s)" % (_psplatform.cext.__file__,
                                                     __version__)
    raise ImportError(msg)

And that creates the following traceback:

Oct 31 21:26:34: File "bootstrap.py", line 969, in _find_and_load
Oct 31 21:26:34: File "bootstrap.py", line 958, in _find_and_load_unlocked
Oct 31 21:26:34: File "bootstrap.py", line 664, in _load_unlocked
Oct 31 21:26:34: File "bootstrap.py", line 634, in _load_backward_compatible
Oct 31 21:26:34: File ":/psutil/__init__.py", line 208, in <module>
Oct 31 21:26:34: AttributeError: module 'psutil_linux' has no attribute '__file__'
Oct 31 21:26:34: 4.4.1
Oct 31 21:26:34: 441
Oct 31 21:26:34: 442

As you can see it detected a 4.4.1 python version and a 4.4.2 C-extension version (which is a mistake I corrected, I used old psutil files with a newer compiled 4.4.2 version of the c-extensions). It then tries to raise an ImportError that, in my case, raises an AttributeError itself because it cannot find _psplatform.cext.__file__.

My app compiles psutil using pyqtdeploy 1.3.1 and Python 3.5.2 that turns it into a native Qt application. This causes the __file__ attribute to be missing I suspect. Can we add a check for this attribute and ignore it when it doesn't exist?

@giampaolo
Copy link
Owner

giampaolo commented Nov 1, 2016

This is weird. What psutil version are you using? The print(_psplatform.cext.__file__) line is not part of psutil source code.

@thijstriemstra
Copy link
Contributor Author

thijstriemstra commented Nov 1, 2016

@giampaolo no, I added those lines here to debug the problem. So these lines were added:

print(__version__)
print(int(__version__.replace('.', '')))
print(getattr(_psplatform.cext, 'version', None))
print(_psplatform.cext.__file__)

with only 3 results printed:

4.4.1
441
442

And that last print produces (print(_psplatform.cext.__file__)) an AttributeError to demonstrate the issue. Which otherwise would be produced in the statement after that:

msg = "version conflict: %r C extension module was built for another " \
          "version of psutil (different than %s)" % (_psplatform.cext.__file__,
                                                     __version__)

This is using 4.4.2 (+these prints). Hope that makes sense..

@giampaolo
Copy link
Owner

giampaolo commented Nov 2, 2016

OK, I fixed this in 58c4b3d and I also provided a better error message printing both versions:

_ImportError: version conflict: '/home/giampaolo/svn/psutil/psutil/_psutil_linux.so' C extension module was built for another version of psutil (4.4.1 instead of 4.4.2); you may try to 'pip uninstall psutil', manually remove /home/giampaolo/svn/psutil/psutil/psutil_linux.so or clean the virtual env somehow, then reinstall

@thijstriemstra
Copy link
Contributor Author

Awesome, thanks @giampaolo!

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

No branches or pull requests

2 participants