I have a very simple program in python that works perfectly within Spyder or Eclipse, but fails when executed in the command line after it was pacakged with py2exe.

OS is a French Windows 7 and I am using: Python 3.4.1 from Anaconda 2.1.0 (64-bit) Pandas 0.15.2 np19py34_0 cython 0.21.2

The error I get is the following one:

Parent module '' not loaded, cannot perform relative import
Traceback (most recent call last):
  File "Main.py", line 2, in <module>
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
  File "C:\Anaconda3\lib\site-packages\zipextimporter.py", line 86, in load_module
    return zipimport.zipimporter.load_module(self, fullname)
  File "C:\Anaconda3\lib\site-packages\pandas\__init__.py", line 7, in <module>
    from . import hashtable, tslib, lib
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
  File "C:\Anaconda3\lib\site-packages\zipextimporter.py", line 109, in load_module self.get_data)
  File "pandas\hashtable.pyx", line 9, in init pandas.hashtable (pandas\hashtable.c:23000)
SystemError: Parent module '' not loaded, cannot perform relative import

Here is the main.py coding:

# -*- coding: utf-8 -*-
import pandas
data = pandas.read_hdf ("C:/temp/test.h5", "MainData")
file = open ("C:/temp/log.txt", "a+")
strMessage = ""
for arg in data.columns:
    strMessage += str (arg) + "\n"
file.write (strMessage)
file.close()

and the setup.py:

# -*- coding: utf-8 -*-
from distutils.core import setup
import py2exe

excludes = ['tkinter']
opts = {'py2exe': {'excludes':excludes,
                   'bundle_files': 1,
                   'compressed': True,
                   'optimize':2}}

setup(name='Core',
      version='1.0',
      description = "Core Engine",
      console = [{"script": "Main.py",
                  "dest_base": "Core"}],
      options = opts)

I'm executing the following command line to build my executable file: python setup.py py2exe

Note that it works fine if I remove the 'bundle_files' and 'optimize' options and I downgrade pandas to version 0.14.2.

Comment From: sakurai-youhei

I made it work after long long time investigation. The problem is rather in py2exe; py2exe doesn't look after "_Py_PackageContext". Here's how I worked around the root cause in my testing.

Build my exe in clean environment

C:\Python34\python.exe -m venv pandas-py2exe
pushd pandas-py2exe
Scripts\activate.bat
pip install pip --upgrade

set BLAS=None
set LAPACK=None
set ATLAS=None
pip install py2exe pandas --no-binary numpy

python -m py2exe.build_exe -b 2 -x tkinter -x sqlite3 test-pandas-py2exe.py

test-pandas-py2exe.py

import ctypes
import functools
import _memimporter

_Py_PackageContext = ctypes.c_char_p.in_dll(
    ctypes.pythonapi,
    "_Py_PackageContext"
)

def patch_import_module(func):
    @functools.wraps(func)
    def inner(fullname, path, initname, get_data):
        if not path.endswith(".dll"):
            old_context = _Py_PackageContext.value
            _Py_PackageContext.value = fullname.encode("ascii")
        try:
            return func(fullname, path, initname, get_data)
        finally:
            if "old_context" in locals():
                _Py_PackageContext.value = old_context
    return inner

_memimporter.import_module = patch_import_module(_memimporter.import_module)

import pandas as pd
print(pd.__version__)

Result and version info

(pandas-py2exe) C:\Users\sakurai\Desktop\pandas-py2exe>dist\test-pandas-py2exe.exe
0.18.1

(pandas-py2exe) C:\Users\sakurai\Desktop\pandas-py2exe>python --version
Python 3.4.3

(pandas-py2exe) C:\Users\sakurai\Desktop\pandas-py2exe>pip list
numpy (1.11.1)
pandas (0.18.1)
pip (8.1.2)
py2exe (0.9.2.2)
python-dateutil (2.5.3)
pytz (2016.6.1)
setuptools (12.0.5)
six (1.10.0)

Note

Because the "_Py_PackageContext" becomes available from py2exe.build_exe -b 2 which is equal to bundle_files=2 in setup.py, even my code doesn't work with neither -b 0 nor -b 1 option. This limitation is also caused by bootstrap tricks made by py2exe.

Comment From: jreback

closing as not a pandas issue