与Pyinstaller和Pandas的重要联系

2024-03-29 09:18:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将一个简短的python脚本打包成一个可执行文件。我可以使用

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

Tags: toinfrompyimportbuild脚本可执行文件
2条回答

编辑.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中有详细说明,它可能不适用于所有版本/操作系统,所以祝您好运。

错误

ImportError: C extension: lib not built.

清楚地告诉您运行python setup.py build_ext --inplace。 构建C扩展

相关问题 更多 >