__init__.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. """
  2. Contains the core of NumPy: ndarray, ufuncs, dtypes, etc.
  3. Please note that this module is private. All functions and objects
  4. are available in the main ``numpy`` namespace - use that instead.
  5. """
  6. from numpy.version import version as __version__
  7. import os
  8. # disables OpenBLAS affinity setting of the main thread that limits
  9. # python threads or processes to one core
  10. env_added = []
  11. for envkey in ['OPENBLAS_MAIN_FREE', 'GOTOBLAS_MAIN_FREE']:
  12. if envkey not in os.environ:
  13. os.environ[envkey] = '1'
  14. env_added.append(envkey)
  15. try:
  16. from . import multiarray
  17. except ImportError as exc:
  18. import sys
  19. msg = """
  20. IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
  21. Importing the numpy C-extensions failed. This error can happen for
  22. many reasons, often due to issues with your setup or how NumPy was
  23. installed.
  24. We have compiled some common reasons and troubleshooting tips at:
  25. https://numpy.org/devdocs/user/troubleshooting-importerror.html
  26. Please note and check the following:
  27. * The Python version is: Python%d.%d from "%s"
  28. * The NumPy version is: "%s"
  29. and make sure that they are the versions you expect.
  30. Please carefully study the documentation linked above for further help.
  31. Original error was: %s
  32. """ % (sys.version_info[0], sys.version_info[1], sys.executable,
  33. __version__, exc)
  34. raise ImportError(msg)
  35. finally:
  36. for envkey in env_added:
  37. del os.environ[envkey]
  38. del envkey
  39. del env_added
  40. del os
  41. from . import umath
  42. # Check that multiarray,umath are pure python modules wrapping
  43. # _multiarray_umath and not either of the old c-extension modules
  44. if not (hasattr(multiarray, '_multiarray_umath') and
  45. hasattr(umath, '_multiarray_umath')):
  46. import sys
  47. path = sys.modules['numpy'].__path__
  48. msg = ("Something is wrong with the numpy installation. "
  49. "While importing we detected an older version of "
  50. "numpy in {}. One method of fixing this is to repeatedly uninstall "
  51. "numpy until none is found, then reinstall this version.")
  52. raise ImportError(msg.format(path))
  53. from . import numerictypes as nt
  54. multiarray.set_typeDict(nt.sctypeDict)
  55. from . import numeric
  56. from .numeric import *
  57. from . import fromnumeric
  58. from .fromnumeric import *
  59. from . import defchararray as char
  60. from . import records as rec
  61. from .records import *
  62. from .memmap import *
  63. from .defchararray import chararray
  64. from . import function_base
  65. from .function_base import *
  66. from . import machar
  67. from .machar import *
  68. from . import getlimits
  69. from .getlimits import *
  70. from . import shape_base
  71. from .shape_base import *
  72. from . import einsumfunc
  73. from .einsumfunc import *
  74. del nt
  75. from .fromnumeric import amax as max, amin as min, round_ as round
  76. from .numeric import absolute as abs
  77. # do this after everything else, to minimize the chance of this misleadingly
  78. # appearing in an import-time traceback
  79. from . import _add_newdocs
  80. # add these for module-freeze analysis (like PyInstaller)
  81. from . import _dtype_ctypes
  82. from . import _internal
  83. from . import _dtype
  84. from . import _methods
  85. __all__ = ['char', 'rec', 'memmap']
  86. __all__ += numeric.__all__
  87. __all__ += fromnumeric.__all__
  88. __all__ += rec.__all__
  89. __all__ += ['chararray']
  90. __all__ += function_base.__all__
  91. __all__ += machar.__all__
  92. __all__ += getlimits.__all__
  93. __all__ += shape_base.__all__
  94. __all__ += einsumfunc.__all__
  95. # Make it possible so that ufuncs can be pickled
  96. # Here are the loading and unloading functions
  97. # The name numpy.core._ufunc_reconstruct must be
  98. # available for unpickling to work.
  99. def _ufunc_reconstruct(module, name):
  100. # The `fromlist` kwarg is required to ensure that `mod` points to the
  101. # inner-most module rather than the parent package when module name is
  102. # nested. This makes it possible to pickle non-toplevel ufuncs such as
  103. # scipy.special.expit for instance.
  104. mod = __import__(module, fromlist=[name])
  105. return getattr(mod, name)
  106. def _ufunc_reduce(func):
  107. from pickle import whichmodule
  108. name = func.__name__
  109. return _ufunc_reconstruct, (whichmodule(func, name), name)
  110. import copyreg
  111. copyreg.pickle(ufunc, _ufunc_reduce, _ufunc_reconstruct)
  112. # Unclutter namespace (must keep _ufunc_reconstruct for unpickling)
  113. del copyreg
  114. del _ufunc_reduce
  115. from numpy._pytesttester import PytestTester
  116. test = PytestTester(__name__)
  117. del PytestTester