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

pkg_resources' detection of Python2 vs Python3 is brittle #237

Closed
ghost opened this issue Aug 4, 2014 · 4 comments
Closed

pkg_resources' detection of Python2 vs Python3 is brittle #237

ghost opened this issue Aug 4, 2014 · 4 comments

Comments

@ghost
Copy link

ghost commented Aug 4, 2014

Originally reported by: anntzer (Bitbucket: anntzer, GitHub: anntzer)


The following part of pkg_resources

#!python
try:
    basestring
    next = lambda o: o.next()
    from cStringIO import StringIO as BytesIO
except NameError:
    basestring = str
    from io import BytesIO
    def execfile(fn, globs=None, locs=None):
        if globs is None:
            globs = globals()
        if locs is None:
            locs = globs
        exec(compile(open(fn).read(), fn, 'exec'), globs, locs)

tries to provide some standard Python2 functions when run with Python3, but is broken if other packages already do the same, but for a different subset. Specifically, if another package already defined (in Python3) builtins.basestring = str, the import of cStringIO will fail with an ImportError, and later calls to next will fail too.


@ghost
Copy link
Author

ghost commented Sep 18, 2014

Original comment by flutefreak7 (Bitbucket: flutefreak7, GitHub: flutefreak7):


This issue caused the cStringIO import error when I imported pandas after importing pyqtgraph on Python 3.4. Pyqtgraph has a python2_3 compatibility module in which builtins.basestring = str is implemented for Python 3. Pandas, during initialization, imports pkg_resources which then throws " ImportError: No module named 'cStringIO' ". For me this means if pyqtgraph and pandas are both imported, pandas should be imported first if possible. That or some kind of check that deletes basestr.

Better yet.... I think I'll just add ImportError to the structure shown above so that the except is triggered when cStringIO isn't found in Python 3.

This needs to utilize sys.version_info or at least handle the ImportError.

@ghost
Copy link
Author

ghost commented Sep 18, 2014

Original comment by flutefreak7 (Bitbucket: flutefreak7, GitHub: flutefreak7):


Dealing with the setuptools egg was a pain so I did this around my pandas import as a workaround for the pkg_resources bug:

#!python

if hasattr(builtins, 'basestring'):
    delattr(builtins, 'basestring')
    import pandas as pd
    builtins.basestring = str
else:
    import pandas as pd

@ghost
Copy link
Author

ghost commented Sep 18, 2014

Original comment by jaraco (Bitbucket: jaraco, GitHub: jaraco):


Use PY3/PY2 indicators to reliably select behavior. Fixes #237

@ghost
Copy link
Author

ghost commented Sep 18, 2014

Original comment by jaraco (Bitbucket: jaraco, GitHub: jaraco):


Released as 5.8.

@ghost ghost added major bug labels Mar 29, 2016
@ghost ghost closed this as completed Mar 29, 2016
This issue was closed.
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

0 participants