rkh
rkh

Reputation: 1791

PYTHONPATH affecting BASH search path

Using Anaconda 2.5.0 (python 2.7) on a relatively clean Ubuntu 15.04 desktop system, I have the following unusual behavior:

If I package my application with cx_Freeze 4.3.4 and add the bin directory to the PYTHONPATH environment variable:

export PYTHONPATH=~/frozen-app/bin

Then, when I mistype a bash command:

sl

Instead of the expected:

The program 'sl' is currently not installed. You can install it by typing:
sudo apt-get install sl

I get a python stack trace:

Traceback (most recent call last):
  File "/usr/lib/command-not-found", line 23, in <module>
    import gettext
  File "/usr/lib/python3.4/gettext.py", line 49, in <module>
    import locale, copy, io, os, re, struct, sys
  File "/usr/lib/python3.4/locale.py", line 18, in <module>
    import collections
  File "/usr/lib/python3.4/collections/__init__.py", line 11, in <module>
    from operator import itemgetter as _itemgetter, eq as _eq
ImportError: dynamic module does not define init function (PyInit_operator)
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 62, in apport_excepthook
    import re, traceback
  File "/usr/lib/python3.4/traceback.py", line 3, in <module>
    import linecache
  File "/usr/lib/python3.4/linecache.py", line 10, in <module>
    import tokenize
  File "/usr/lib/python3.4/tokenize.py", line 29, in <module>
    import collections
  File "/usr/lib/python3.4/collections/__init__.py", line 11, in <module>
    from operator import itemgetter as _itemgetter, eq as _eq
ImportError: dynamic module does not define init function (PyInit_operator)

Original exception was:
Traceback (most recent call last):
  File "/usr/lib/command-not-found", line 23, in <module>
    import gettext
  File "/usr/lib/python3.4/gettext.py", line 49, in <module>
    import locale, copy, io, os, re, struct, sys
  File "/usr/lib/python3.4/locale.py", line 18, in <module>
    import collections
  File "/usr/lib/python3.4/collections/__init__.py", line 11, in <module>
    from operator import itemgetter as _itemgetter, eq as _eq
ImportError: dynamic module does not define init function (PyInit_operator)

It seems that the "command-not-found" command gets confused by my PYTHONPATH environment, but I can't figure out exactly why. I don't think any of the mentioned modules are defined in my application (I don't define an "re" module in my app for example).

If I run "strace sl", then I just get "can't stat sl". Is there another way to figure out exactly which file in my application is causing this?

Upvotes: 2

Views: 298

Answers (1)

rkh
rkh

Reputation: 1791

Using the suggestion by Charles Duffy above, and looking more closely at the python stack trace, I was able to reproduce the error with:

python3.4 -m operator

Just this replicates the stack trace. Since my PYTHONPATH points to a bin directory from from an Anaconda2.5 (python 2.7), it seems that the python3.4 tries to load operator.so from the the anaconda distribution which has been frozen in my bin directory. This particular import fails. Deleting operator.so from my bin fixes this issue. It seems that using the system python3.4 (which is used by command-not-found) and the Anaconda 2.7 combined with explicitly setting PYTHONPATH can cause trouble.

Upvotes: 1

Related Questions