PyInstaller on mac can't find libpython2.7

2019-07-13 06:58发布

问题:

I am trying to make a binary version of a Python script using PyInstaller 2.0. I am using a basic "hello world" tkinter script but imported a few dependencies that i need for a project to test Pyinstaller out. I am on a mac running Yosemite 10.10.5. This is my script:

#!/usr/bin/env python
from Tkinter import *
import Tix
import tkMessageBox
from sklearn import linear_model, decomposition, preprocessing
from sklearn.preprocessing import Imputer
from sklearn.cross_validation import cross_val_score, cross_val_predict
from sklearn.neighbors import KDTree 
import numpy as np
import collections
import array
import math
import csv
from collections import OrderedDict
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
import matplotlib.dates as dates
from matplotlib.mlab import PCA
from mpl_toolkits.mplot3d import Axes3D
from scipy.stats import mode
import heapq
import sqlite3
from sqlite3 import datetime


root = Tk()

w = Label(root, text="Hello, world!")
w.pack()

root.mainloop()

This runs perfectly. However when i go to build the binary using

$pyinstaller -w -F app.py 

then i get this error:

57665 ERROR: Can not find path ./libpython2.7.dylib (needed by //anaconda/bin/python)
Traceback (most recent call last):
  File "//anaconda/bin/pyinstaller", line 11, in <module>
    sys.exit(run())
  File "//anaconda/lib/python2.7/site-packages/PyInstaller/__main__.py", line 90, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "//anaconda/lib/python2.7/site-packages/PyInstaller/__main__.py", line 46, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "//anaconda/lib/python2.7/site-packages/PyInstaller/building/build_main.py", line 788, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "//anaconda/lib/python2.7/site-packages/PyInstaller/building/build_main.py", line 734, in build
    exec(text, spec_namespace)
  File "<string>", line 16, in <module>
  File "//anaconda/lib/python2.7/site-packages/PyInstaller/building/build_main.py", line 212, in __init__
    self.__postinit__()
  File "//anaconda/lib/python2.7/site-packages/PyInstaller/building/datastruct.py", line 178, in __postinit__
    self.assemble()
  File "//anaconda/lib/python2.7/site-packages/PyInstaller/building/build_main.py", line 543, in assemble
    self._check_python_library(self.binaries)
  File "//anaconda/lib/python2.7/site-packages/PyInstaller/building/build_main.py", line 626, in _check_python_library
    raise IOError(msg)
IOError: Python library not found: libpython2.7.dylib, Python, .Python
This would mean your Python installation doesn't come with proper library files.
This usually happens by missing development package, or unsuitable build parameters of Python installation.

* On Debian/Ubuntu, you would need to install Python development packages
  * apt-get install python3-dev
  * apt-get install python-dev
* If you're building Python by yourself, please rebuild your Python with `--enable-shared` (or, `--enable-framework` on Darwin)

Does anyone have any ideas how i can fix this? This error also occurs when i am using the the basic hello world example without the extra dependancies. I have the libpython2.7.dylib file in //anaconda/lib and i tried to link it to usr/lib/ using

$sudo ln -s /usr/local/lib/libpython2.7.dylib //anaconda/lib/libpython2.7.dylib

however it is not fixing the issue...

回答1:

Firstly, I see you are using conda. I ran into the exact same issue on Mac, specifically:

ERROR: Can not find path ./libpython2.7.dylib

trying to deploy an app I put together in a conda environment.

After a lot of Googling and reading, I found that the current PyInstaller does not handle dynamic libraries with @rpath references very well. You can confirm that the library reference uses @rpath by running "otool -L" on the Python binary, which for you looks like //anaconda/bin/python (might be a link to //anaconda/bin/python2.7).

Fortunately, this was recently addressed on a fork of PyInstaller for conda. The specific patch is at https://github.com/conda-forge/pyinstaller-feedstock/pull/2

What I did to use this forked version is uninstall PyInstaller that I had downloaded in my conda environment via pip, and then used the instructions from https://github.com/conda-forge/pyinstaller-feedstock to use this fork of PyInstaller in my conda environment. Specifically, these commands:

conda config --add channels conda-forge
conda install pyinstaller

So I'd recommend switching to this patched version of PyInstaller specifically for conda environments, and see if you it helps you get past the problem like it did for me.



回答2:

If you are using python via pyenv like me, you might need to reinstall with enabling shared to access xcode libs unless you had done that earlier.

sudo env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 2.7

PS: I am on Darwin but still enable-shared worked than enable-framework

In fact the message below error tells what to do