Pyinstaller 和 Pandas 的导入错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33001327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:59:57  来源:igfitidea点击:

ImportError with Pyinstaller and Pandas

pythonpandaspyinstaller

提问by gwaldman13

I am trying to bundle a short python script into a single executable. I am able to successfully run pyinstaller using

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

pyinstaller script.py

However, when I run the executable I get the following error. I have tried everything and nothing seems to work.

但是,当我运行可执行文件时,出现以下错误。我已经尝试了一切,但似乎没有任何效果。

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

Here are the imports in my script:

这是我的脚本中的导入:

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

回答by Zero

Edit your .specfile to add the lines shown below just after the a = Analysis part. Then build using the --onefile flag– e.g., pyinstaller --onefile my_project.spec

编辑您的.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)

The reason this is necessary is PyInstaller is grabbing pandas python code, but not grabbing the lib. This means that when the pandas code runs (from 'inside' the executable) it can't find the lib – so it tries to be helpful and suggest you need to build it.

这是必要的原因是 PyInstaller 正在抓取 Pandas python 代码,而不是抓取 lib。这意味着当 Pandas 代码运行时(从可执行文件的“内部”)它找不到 lib——所以它试图提供帮助并建议你需要构建它。

The workaround is detailed at http://github.com/pyinstaller/pyinstaller/issues/1580– it appears it might not work for all versions / operating systems, so best of luck.

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

回答by DanHabib

The error

错误

ImportError: C extension: lib not built.

Clearly tells you to run python setup.py build_ext --inplace. to build the C extensions

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