Search code examples
pythonpython-module

Embedded Python search modules in a wrong directory


I have installed Python 2.6.7 in $HOME/local of a machine which already has a default Python in /usr (I don't have admin access on this machine). The default Python is compiled in 32bits and my local installation is a 64bits. For some unknown reasons my local Python library (which I call as an embedded python interpreter from within a C program) search for the modules in the default (wrong) installation.

This is the result of "import random"

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    import random
  File "/tmp/work/mdorier1/local/lib/python2.6/random.py", line 45, in <module>
    from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
ImportError: /usr/lib/python2.6/lib-dynload/math.so: wrong ELF class: ELFCLASS32

As you can see, the import statement correctly search "random.py" in the local installation of Python, but the import statement in random.py go search for math.so in the wrong location, which ends in an error since the default location has 32 bits modules.

I guessed there is a problem with an environment variable, and I tried

import sys
sys.path

to get

['/tmp/work/mdorier1/local/lib/python26.zip', 
'/tmp/work/mdorier1/local/lib/python2.6', 
'/tmp/work/mdorier1/local/lib/python2.6/plat-linux2', 
'/tmp/work/mdorier1/local/lib/python2.6/lib-tk', 
'/tmp/work/mdorier1/local/lib/python2.6/lib-old', 
'/usr/lib/python2.6/lib-dynload', 
'/tmp/work/mdorier1/local/lib/python2.6/site-packages']

I noticed that on of the paths indeed points to the default installation of Python. My questions thus are: - Why this path shows up here, as the local installation has nothing to do with the default one? - How do I change it (in a clean and permanent way)? This path should be the path to lib-dynload in the local installation instead.

Thanks


Solution

  • You probably don't have your Python's bin directory in the PATH variable before the system Python.

    Or perhaps you simply compiled your Python incorrectly and did not use:

    ./configure --prefix=/tmp/work/mdorier1/local
    

    so now it thinks that its files are somewhere else.