找到你要的答案

Q:ImportError with Pyinstaller and Pandas

Q:Pyinstaller和大熊猫输入错误

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

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

我试图捆绑成一个可执行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
answer1: 回答1:

Edit you spec file, to add the lines shown below just after the a = Analysis part. Then build using the --onefile flag, eg 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) is can't find the lib - so it tries to be helpful and suggest you need to build it. The workaroudn is detailed here github.com/pyinstaller/pyinstaller/issues/1580 - it appears it might not work for all version / operating systems, so best of luck.

编辑您的规格文件,添加下面显示的行如下=分析部分。然后建立使用--一个文件标志,如pyinstaller --一个文件,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抓熊猫的Python代码,但没有抓住自由。这意味着,当大熊猫代码运行(从'内'可执行文件)是无法找到库-所以它试图帮助,并建议你需要建立它。这里的workaroudn详细github.com/pyinstaller/pyinstaller/issues/1580似乎可能不是所有版本操作系统的工作,所以最好的运气。

answer2: 回答2:

The error

ImportError: C extension: lib not built.

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

错误

ImportError: C extension: lib not built.

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

python  pandas  pyinstaller