Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy Pyinstaller ImportError: cannot import name multiarray

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)
like image 508
Praxis Avatar asked Sep 28 '15 10:09

Praxis


2 Answers

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')
like image 52
J Richard Snape Avatar answered Nov 15 '22 07:11

J Richard Snape


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\

like image 40
weeshin Avatar answered Nov 15 '22 09:11

weeshin