【问题标题】:ImportError with Pyinstaller and PandasPyinstaller 和 Pandas 的 ImportError
【发布时间】:2016-01-05 05:19:36
【问题描述】:

我正在尝试将一个简短的 python 脚本捆绑到一个可执行文件中。我能够使用

成功运行 pyinstaller
pyinstaller script.py

但是,当我运行可执行文件时,我收到以下错误。我已经尝试了所有方法,但似乎没有任何效果。

C:\Users\...\Python\dist\script>script
Traceback (most recent call last):
  File "<string>", line 2, in <module>
  File "c:\users\user\appdata\local\temp\pip-build-0pjuke\pyinstaller\PyInst
aller\loader\pyimod03_importers.py", line 363, in load_module
  File "c:\python27\lib\site-packages\pandas\__init__.py", line 13, in <module>
    "extensions first.".format(module))
ImportError: C extension: lib not built. If you want to import pandas from the s
ource directory, you may need to run 'python setup.py build_ext --inplace' to bu
ild the C extensions first.
script returned -1

这是我脚本中的导入:

import pandas
from simple_salesforce import Salesforce
from pandas import Series, DataFrame
import vertica_python
from StringIO import StringIO

【问题讨论】:

  • 你试过运行python setup.py build_ext --inplace
  • PyInstaller 正在抓取 pandas python 代码,但没有抓取 lib。这意味着当 pandas 代码运行时(从可执行文件的“内部”)找不到 lib - 所以它试图提供帮助并建议您需要构建它。这方面的一些工作是github.com/pyinstaller/pyinstaller/issues/1580,但我自己没有任何成功。

标签: python pandas pyinstaller


【解决方案1】:

编辑您的.spec 文件,在a = Analysis part 之后添加如下所示的行。然后使用--onefile flag 构建——例如,pyinstaller --onefile my_project.spec

a = Analysis(...)    

# Add the following
def get_pandas_path():
    import pandas
    pandas_path = pandas.__path__[0]
    return pandas_path


dict_tree = Tree(get_pandas_path(), prefix='pandas', excludes=["*.pyc"])
a.datas += dict_tree
a.binaries = filter(lambda x: 'pandas' not in x[0], a.binaries)

这是必要的原因是 PyInstaller 正在抓取 pandas python 代码,但没有抓取 lib。这意味着当 pandas 代码运行时(从可执行文件的“内部”)它找不到 lib - 因此它会尝试提供帮助并建议您需要构建它。

解决方法在http://github.com/pyinstaller/pyinstaller/issues/1580 中有详细说明 - 似乎它可能不适用于所有版本/操作系统,所以祝你好运。

【讨论】:

    【解决方案2】:

    错误

    ImportError: C extension: lib not built.
    

    明确告诉你运行python setup.py build_ext --inplace。 构建 C 扩展

    【讨论】:

    • 你在virtualenv工作吗? virtualenv.pypa.io/en/latest 使用其中之一将解决问题
    • 嗨@gwaldman13,我也有同样的问题,你解决了吗?
    猜你喜欢
    • 2013-07-06
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    • 2017-06-19
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多