6

I'm encountering a similar problem to the one reported here, which seems to be a an unresolved issue.

After compiling an exe, a pyinstaller build throws the following error, which is likely caused by the numpy\core\ init.py

There are some suggestions that it has to do with conflicting numpy installations however I have uninstalled and re-installed several times and searched for any other installations without luck. Currently running with numpy-1.9+MKL binaries.

I have also flagged the multiarray.pyd file into the spec file as a binary to grab. No luck.

No idea what is causing this as I'm not particularly familiar with the init file structure. Any idea how to get this imported?

Traceback of the error:

Traceback (most recent call last):
  File "<string>", line 50, in <module>
  File "C:\Python27\Lib\site-packages\PyInstaller\loader\pyi_importers.py", line 270, in load_module
    exec(bytecode, module.__dict__)
  File "C:\Users\Hp\PycharmProjects\GISdev\build\gis_helper2\out00-PYZ.pyz\mpl_toolkits.basemap", line 15, in <module>
  File "C:\Python27\Lib\site-packages\PyInstaller\loader\pyi_importers.py", line 270, in load_module
    exec(bytecode, module.__dict__)
  File "C:\Users\Hp\PycharmProjects\GISdev\build\gis_helper2\out00-PYZ.pyz\matplotlib", line 133, in <module>
  File "C:\Python27\Lib\site-packages\PyInstaller\loader\pyi_importers.py", line 270, in load_module
    exec(bytecode, module.__dict__)
  File "C:\Users\Hp\PycharmProjects\GISdev\build\gis_helper2\out00-PYZ.pyz\matplotlib.rcsetup", line 19, in <module>
  File "C:\Python27\Lib\site-packages\PyInstaller\loader\pyi_importers.py", line 270, in load_module
    exec(bytecode, module.__dict__)
  File "C:\Users\Hp\PycharmProjects\GISdev\build\gis_helper2\out00-PYZ.pyz\matplotlib.colors", line 52, in <module>
  File "C:\Python27\Lib\site-packages\PyInstaller\loader\pyi_importers.py", line 270, in load_module
    exec(bytecode, module.__dict__)
  File "C:\Users\Hp\PycharmProjects\GISdev\build\gis_helper2\out00-PYZ.pyz\numpy", line 200, in <module>
  File "C:\Python27\Lib\site-packages\PyInstaller\loader\pyi_importers.py", line 270, in load_module
    exec(bytecode, module.__dict__)
  File "C:\Users\Hp\PycharmProjects\GISdev\build\gis_helper2\out00-PYZ.pyz\numpy.add_newdocs", line 13, in <module>
  File "C:\Python27\Lib\site-packages\PyInstaller\loader\pyi_importers.py", line 270, in load_module
    exec(bytecode, module.__dict__)
  File "C:\Users\Hp\PycharmProjects\GISdev\build\gis_helper2\out00-PYZ.pyz\numpy.lib", line 8, in <module>
  File "C:\Python27\Lib\site-packages\PyInstaller\loader\pyi_importers.py", line 270, in load_module
    exec(bytecode, module.__dict__)
  File "C:\Users\Hp\PycharmProjects\GISdev\build\gis_helper2\out00-PYZ.pyz\numpy.lib.type_check", line 11, in <module>
  File "C:\Python27\Lib\site-packages\PyInstaller\loader\pyi_importers.py", line 270, in load_module
    exec(bytecode, module.__dict__)
  File "C:\Users\Hp\PycharmProjects\GISdev\build\gis_helper2\out00-PYZ.pyz\numpy.core", line 14, in <module>
ImportError: cannot import name multiarray

Possible cause of the problem taken from the init file:

from __future__ import division, absolute_import, print_function

from .info import __doc__
from numpy.version import version as __version__

# disables OpenBLAS affinity setting of the main thread that limits
# python threads or processes to one core
import os
envbak = os.environ.copy()
if 'OPENBLAS_MAIN_FREE' not in os.environ:
    os.environ['OPENBLAS_MAIN_FREE'] = '1'
if 'GOTOBLAS_MAIN_FREE' not in os.environ:
    os.environ['GOTOBLAS_MAIN_FREE'] = '1'
from . import multiarray
os.environ.clear()
os.environ.update(envbak)
del envbak
del os

from . import umath
from . import _internal  # for freeze programs
from . import numerictypes as nt
multiarray.set_typeDict(nt.sctypeDict)
Community
  • 1
  • 1
Praxis
  • 934
  • 2
  • 17
  • 31
  • I have a feeling this is related to the lack of .py files in the numpy.core module. Only pyd files exist, yet the init file is trying to import the module multiarray. – Praxis Sep 30 '15 at 12:15
  • @Richard Snape - The code runs fine in python 2.7 both from console and Pycharm. It's only when when the exe is run that the error occurs so I think it's a numpy-pyinstaller compatability issue. I'm wondering if decompiling from the .pyd files to .py might be a solution. The source of the error is. Line 14 in init.py calls: "from . import multiarray" Numpy can obviously handle loading the .pyd dynamically when not compiled, but may require a .py file when it is is frozen in an exe. – Praxis Oct 01 '15 at 09:02
  • Thanks Richard, I would really appreciate your help on this one – Praxis Oct 01 '15 at 12:18
  • 1
    Some things you could add to the question to make diagnosis a bit quicker: post the contents of `.spec` file for your project (i.e. in the directory where you run `pyinstaller`, you'll have a file called `GISdev.spec` or similar. Try running with `--onedir` mode - see [here](https://github.com/pyinstaller/pyinstaller/wiki/How-to-Report-Bugs#make-sure-everything-is-packaged-correctly) for details. I remember finding a non-packaged piece of a library via that route once... – J Richard Snape Oct 01 '15 at 16:56
  • 1
    OK - I got some time on this. I just did a clean install of `pyinstaller` on Windows (assumed from your path name) and a minimal file consisting of `import numpy as np` followed by `print numpy.__version__`. This All Just Worked™. I used Python 2.7 (again assumed from your paths) and numpy 1.9. Can you a minimal version of ***your*** python code around where it fails - look at http://stackoverflow.com/help/mcve for help on how to make a minimal case that fails in the way you describe. – J Richard Snape Oct 02 '15 at 14:32
  • Hey, ok so I tried those other links you suggested and had no luck unfortunately. I have stripped back the file to almost exactly as you have specified. `import numpy as np` and `(print(np.version.version))` and the exe results with the same error. Running with numpy 1.9, and (yes correct) python 2.7 on windows. This must mean it's an install or config error on my machine I am assuming? Pyinstaller version is 2.1. Is there anything to do other than just pip uninstall to do a fresh install? – Praxis Oct 02 '15 at 15:56
  • 1
    Sorry, I haven't much time this evening. One further question - you are running the exe out of the `dist` directory not `build`, right? If not - that could be your problem (although I'd expect it to fail earlier with "can't find Win32" or similar. If that's not the problem - you could try a `pip uninstall` and re-install that's a useful thing to eliminate at least. – J Richard Snape Oct 02 '15 at 19:49
  • OK, I think I've solved it. So after you suggested that I use a minimal version. I went back and created a fresh `spec` file and began adding in the lines from my original `spec` . In my spec file I had replaced the `a.binaries` file with `a.binaries1` to iterate some .dll additions. Seems that this wasn't a problem until it came to `numpy.core`. Now it works and the exe has finally compiled (mostly). Thank you for your help J Richard. I've learned a lot about testing pyinstaller. – Praxis Oct 03 '15 at 03:32
  • 1
    Glad to help - do you want to write up an answer or shall I? I note you say "(mostly)". I hope you worked out the last corners. If the problem is that you now can't include some binaries you want - post your `.spec` file and we can work it out. `a.binaries` is a member of the `Analysis` object, rather than a file. You might want to look at http://pythonhosted.org/PyInstaller/#adding-files-to-the-bundle. It's a good idea to always post the `.spec` with pyinstaller questions as I said above - otherwise – J Richard Snape Oct 03 '15 at 11:05
  • Thanks J Richard, I was just about to say please write up an answer since it was your advice that solved the problem. I have a new hurdle in the script which I am just investigating regarding gdal dependencies. Will post a question with '.spec' soon. The (mostly) part however is that I have numpy, scipy and matplotlib functioning in pyinstaller now and the exe actually executes. Most of the battle won! – Praxis Oct 04 '15 at 08:13
  • @J Richard Snape Have posted the follow up question [here](http://stackoverflow.com/questions/32931976/pyinstaller-calling-gdal-from-os-system-gdal-translate) – Praxis Oct 04 '15 at 09:29

2 Answers2

3

After an exchange in comments, the problem was isolated to a problem in a custom .spec file used by the OP. In the .spec, a line something like:

coll = COLLECT(exe,
           a.binaries,
           a.zipfiles,
           a.datas,
           strip=None,
           upx=True,
           name='nptest')

had been replaced with

coll = COLLECT(exe,
           a.binaries1,
           a.zipfiles,
           a.datas,
           strip=None,
           upx=True,
           name='nptest')

to try to introduce a file a.binaries1 to enable pyinstaller to use some custom .dll binaries.

In face a.binaries is a member of the Analysis object and needs to remain - the way to add an extra binary gile in the collect line is like this (as per the docs). note you can change the name of the file in your distribution (if needed) by altering the first member of the tuple.

coll = COLLECT(exe,
           a.binaries+[('zipcontainer.dll','C:\\Windows\\System32\\zipcontainer.dll','BINARY')],
           a.zipfiles,
           a.datas,
           strip=None,
           upx=True,
           name='nptest')
J Richard Snape
  • 20,116
  • 5
  • 51
  • 79
1

I am using Pycharm IDE and Anaconda on 64 bit Windows 10.

I have solve the problem by following sequences:

  1. uninstall the numpy in Anaconda;
  2. delete the related numpy files in the folder C:\Users\(COMPUTER NAME)\AppData\Roaming\Python\Python35\site-packages
  3. reinstall numpy in Anaconda

In your case, I suppose you could reinstall numpy after deleting the files in the folder C:\Python27\Lib\site-packages\PyInstaller\loader\

ah bon
  • 9,293
  • 12
  • 65
  • 148
weeshin
  • 43
  • 1
  • 7