PyInstaller cannot find libpython2.7.so when making binary?

14,972

Solution 1

In the server I am using, there's only /usr/lib/python2.6 and not /usr/lib/python2.7 but python 2.7 is used routinely by me and is functional, etc. so I don't see it why it would be a problem to find its libraries. There is a /usr/local/lib/libpython2.7.a.

If there is no libpython2.7.so, of course it will be a problem to find that library.

The reason you're able to use the Python interpreter is probably that it's statically linked.

There are two ways to solve this.

First, you could just install the shared libraries for your Python 2.7. If you're using an older version of Ubuntu that came with, say, Python 2.6, and you installed 2.7 from the python2.7 package, this should just be a matter of installing libpython2.7.

If you've gotten your 2.7 from some other source, the Ubuntu libpython2.7 package obviously won't work—or it'll work by installing a second copy of python2.7, possibly overwriting some of the files you already have, and definitely confusing you. Either way, don't do it. Either get the rest of Python for your existing 2.7, or uninstall that 2.7 and use the Ubuntu packages. (For some Python distributions, "get the rest of it" is impossible, because if you install the shared libs, you get a dynamically-linked Python executable instead of your statically-linked one. In that case, you pretty much have to uninstall and reinstall.)

Second, you could use PyInstaller's static-lib support. See ticket 420 for details, but the simple idea is that, if this is enabled, and PyInstaller thinks your platform should have a libpython2.7.so but can't find it, it will look for a libpython2.7.a and statically link that instead. Last time I needed this, it wasn't checked into trunk. From a quick glance at the ticket, it looks like the patch is now included, but disabled in default PyInstaller builds, and the milestone is set to 3.0, so, you may still have to manually build PyInstaller to get this to work.

One last thing: It's possible that you do have libpython2.7.so, but it's just installed somewhere weird like /opt/python27/lib or something, with /opt/python27 nowhere on your path, but /usr/local/bin/python27 can find it because it's explicitly built to get stuff out of /opt/python27. This kind of thing tends to be a problem only for Mac users with MacPorts or Fink, not Linux users, but it's not impossible. You can look at the dl table for /usr/local/bin/python27 if you think this might be the issue.

Solution 2

There are 2 options: libpython*.so exists or doesn't exists on your system. You can check it by any find utility starting from root directory. In case the file already exists but still can't be found by PyInstaller: the most generic advice is just to open PyInstaller code and find the module that responsible to find this library. It can be done with simple editor. Than go to this module and edit him to understand what is wrong with your specific system. The code is simple and premature - it will take you ~ 5 minutes to understand the reason. In my case I just added LD_LIBRARY_PATH=/usr/local/lib to my user profile (.bash_profile) and ensured that this *.so file is inside. In case the file isn't on your system or you have incorrect version: just reinstall the python.

Solution 3

As @abarnert already said, the problem seems to be a static compilation of python. To solve this issue is needed to recompile python but adding the flag --enable-shared this time:

    [root@machine ~]# ./configure --prefix=/usr/local --enable-shared
    [root@machine ~]# make && make altinstall

Once you do this, you'll find the requested library (libpython2.7.so.1.0) under /usr/local/lib path so don't forget to add that folder to the $LD_LIBRARY_PATH environment variable:

    [root@machine tmp]$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
Share:
14,972
Admin
Author by

Admin

Updated on June 13, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm trying to make a binary version of a Python script using PyInstaller 2.0 on Linux. When I run:

    $ python pyinstaller.py myscript.py
    

    I get the error:

    8907 INFO: Looking for Python library libpython2.7.so
    Traceback (most recent call last):
    ...
      File "pyinstaller.py", line 91, in <module>
        raise IOError("Python library not found!")
    IOError: Python library not found!
    

    How can this be fixed?

    I am using:

    Linux #98-Ubuntu x86_64 GNU/Linux

    With python 2.7. There are other Pythons on the system but I have it set that:

    alias python="python2.7"
    

    In the server I am using, there's only /usr/lib/python2.6 and not /usr/lib/python2.7 but python 2.7 is used routinely by me and is functional, etc. so I don't see it why it would be a problem to find its libraries. There is a /usr/local/lib/libpython2.7.a.

  • Admin
    Admin over 11 years
    You write "you could just install the shared libraries for your Python 2.7" -- how do I go about doing this? Would it be easier just to install my own local version of Python 2.7 and use that rather than the globally available one?
  • abarnert
    abarnert over 11 years
    Did you read the rest of the paragraph? "If… you installed 2.7 from the [Ubuntu] python2.7 package, this should just be a matter of installing libpython2.7." If you installed 2.7 from a different source, I obviously can't answer that question unless you tell me how you installed it.
  • Admin
    Admin over 11 years
    I certainly did and I still don't know how to install libpython 2.7. Is that standard procedure? I can't install anything in /usr/* since I am not the sys admin here.
  • abarnert
    abarnert over 11 years
    @user248237: OK, if you can't get the admin to install the libpython2.7 package, and you can't use 2.6, then yes, you'll have to install a separate copy of Python 2.7 in your user directory (and make sure not to use the system copy—there will be some confusion to deal with). You may still be able to do this with apt/dpkg (see this question, or you may wave to install from source. Either way, this is probably a question for, e.g., superuser.com or askubuntu.com, not SO.
  • Fernando Martin
    Fernando Martin over 8 years
    Someone said that this answer wasn't useful at some point, Would you please explain why? It's been tested and, at least in one similar situation, it actually solves the reported error in this question so honestly I though it might be helpful for somebody.