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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With