8

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
gwaldman13
  • 121
  • 1
  • 1
  • 5
  • 1
    did you try running `python setup.py build_ext --inplace` – DanHabib Oct 07 '15 at 19:58
  • 1
    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. Some workaounrds for this are https://github.com/pyinstaller/pyinstaller/issues/1580 but I'm not having any success myself. – Zero Mar 22 '16 at 05:11

2 Answers2

13

Edit your .spec file 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

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.

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.

feetwet
  • 3,248
  • 7
  • 46
  • 84
Zero
  • 11,593
  • 9
  • 52
  • 70
-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

DanHabib
  • 824
  • 4
  • 15
  • 25