title: Embedded python in C : Is there a way to import numpy properly from zipped python archive?
body: <p>Seems like I can not import numpy properly from within C application which embeds python, when using local .zip python file containing all the python files and binaries rather then standard python installation. This is the error I'm getting :</p>
  
  <blockquote>
    <p>zipimport: zlib available<br>
    python27.zip\Lib\site-packages\numpy_globals.pyc has bad mtime<br>
    zipimport: zlib available<br>
    import numpy._globals # loaded from Zip python27.zip\Lib\site-packages\numpy_globals.py<br>
    zipimport: zlib available<br>
    python27.zip\Lib\site-packages\numpy__config__.pyc has bad mtime<br>
    zipimport: zlib available<br>
    import numpy.<strong>config</strong> # loaded from Zip python27.zip\Lib\site-packages\numpy__config__.py<br>
    zipimport: zlib available<br>
    python27.zip\Lib\site-packages\numpy\version.pyc has bad mtime<br>
    zipimport: zlib available<br>
    import numpy.version # loaded from Zip python27.zip\Lib\site-packages\numpy\version.py<br>
    zipimport: zlib available<br>
    python27.zip\Lib\site-packages\numpy_import_tools.pyc has bad mtime<br>
    zipimport: zlib available
    import numpy._import_tools # loaded from Zip python27.zip\Lib\site-packages\numpy_import_tools.py<br>
    zipimport: zlib available<br>
    python27.zip\Lib\site-packages\numpy\add_newdocs.pyc has bad mtime<br>
    zipimport: zlib available<br>
    zipimport: zlib available<br>
    import math # builtin<br>
    zipimport: zlib available<br>
    import numpy.lib.info # loaded from Zip python27.zip\Lib\site-packages\numpy\lib\info.pyc<br>
    zipimport: zlib available<br>
    zipimport: zlib available<br>
    zipimport: zlib available<br>
    python27.zip\Lib\site-packages\numpy\core\info.pyc has bad mtime<br>
    zipimport: zlib available<br>
    import numpy.core.info # loaded from Zip python27.zip\Lib\site-packages\numpy\core\info.py<br>
    import numpy.core # loaded from Zip python27.zip\Lib\site-packages\numpy\core__init__.pyc<br>
    import numpy.lib.type_check # loaded from Zip python27.zip\Lib\site-packages\numpy\lib\type_check.pyc<br>
    import numpy.lib # loaded from Zip python27.zip\Lib\site-packages\numpy\lib__init__.pyc<br>
    import numpy.add_newdocs # loaded from Zip python27.zip\Lib\site-packages\numpy\add_newdocs.py<br>
    import numpy # loaded from Zip python27.zip\Lib\site-packages\numpy__init__.py<br>
    <strong><em>cannot import name multiarray</em></strong></p>
  </blockquote>
  
  <p>But when I extract abovementioned .zip file, and run python.exe interpreter and import numpy, everything works fine.</p>
  
  <p>I've build python 2.7.13 from source, x86 Release. After that installed <strong>numpy-1.11.3+mkl-cp27-cp27m-win32.whl</strong> file from <a href="http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy" rel="noreferrer">here</a>, then made a python .zip archive with all neccessary files following common folder structure.</p>
  
  <p>This is how my C code looks like :</p>
  
  <pre><code>int main(int argc, char **argv)
  {
      Py_VerboseFlag++;
      Py_NoSiteFlag++;
  
      Py_SetProgramName(argv[0]);
      Py_SetPythonHome(".");
  
      Py_Initialize();
  
      PyRun_SimpleString("import sys");
      PyRun_SimpleString("sys.path = ['.','python27.zip','python27.zip/DLLs','python27.zip/Lib', 'python27.zip/Lib/site-packages']");;
      PyRun_SimpleString("print sys.path");
  
      printf("\n");
  
      char filename[_MAX_PATH];
      _fullpath(filename, "mod1.py", _MAX_PATH);
  
      PyObject* main_module = PyImport_AddModule("__main__");
      PyObject* main_dict = PyModule_GetDict(main_module);
      PyObject *pyFileObj = PyFile_FromString(filename, "r");
  
      if (pyFileObj == NULL) {
          return -1;
      }
  
      FILE *pFile = PyFile_AsFile(pyFileObj);
  
      if (pFile == NULL)
          return -1;
  
      PyObject *result = PyRun_File(pFile, filename, Py_file_input, main_dict, main_dict);
  
      if (!result)
          print_error();
  
      printf("\n\n");
  
  
      Py_Finalize();
      getchar();
  
      return 0;
  }
  </code></pre>
  
  <p>module mod1.py, I'm executing contains this :</p>
  
  <pre><code>import time
  import json
  import numpy
  
  if __name__ == "__main__":
      print 'Success'
  </code></pre>
  
  <p>I'm aware that that error comes from the fact that python can not load <strong>multiarray.pyd</strong> which is shared library.Do I need to handle this scenario separately when it comes to import?<br>
  Is it possible to import numpy properly from zipped, non-standard python installation from C application?</p>
  
  <p><strong>EDIT</strong> : Forgot to mention following :<br>
   - My OS is <strong>Windows 10 x64 Version 1511 (OS Build 10586.545)</strong><br>
   - When I extract .zip archive in my Release folder where my C app binary resides, everything works fine, numpy is loaded properly with following <strong>sys.path</strong> </p>
  
  <pre><code>PyRun_SimpleString("import sys");
  PyRun_SimpleString("sys.path = ['.', 'DLLs', 'Lib', 'Lib\\site-packages', 'Sources']");
  PyRun_SimpleString("print sys.path");
  </code></pre>
  
  <p><strong>EDIT 2</strong>: Also, thought to check exact version of <strong>python27.dll</strong> the <strong>multiarray.pyd</strong> is linked to (according to other posts about similar problems) and it turned out it is different then mine - it's 2.7.8. No problem, extracted specific python27.dll v2.7.8 binary from the .msi package and replaced mine which was 2.7.13, but still no luck. It is definitely something about <strong>zipimport</strong> module.</p>
  
  <p><a href="https://i.stack.imgur.com/ly6F1.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ly6F1.png" alt="enter image description here"></a></p>