Search code examples
pythonpython-2.7easy-installcpythonegg

After using easy_install to install an egg, I get an ImportError when importing the module


We produce 2 Python applications that are distributed as egg files. We've produced them under Python 2.5.4 for years, but are now in the process of bringing them both up to 2.7.13. In this process, 1 of the 2 applications is now exhibiting weird behavior after install on 2.7.13.

When I easy_install.exe the egg, it appears to be successful in every way that I know how to look. However, when I try to import the module at the Python command line, I get an ImportError.

There are many strange things about this problem:

  • This only happens on Windows -- both apps install and run fine on Linux
  • This only happens with 1 of our 2 programs -- the other one runs fine everywhere
  • easy_install does not complain at all during the problematic install on Windows
  • the egg does appear in the path when I do "import sys / print sys.path"
  • the egg does appear in the pth package list in site_packages
  • I'm doing the entire install and testing as the Windows Administrator, so I don't think there could possibly be permissions problems
  • We've made no changes to the source or the setup.py of either program when going from 2.5 to 2.7
  • There's nothing fancy about the install -- no virtual env, just a global install into site_packages

Here's the setup.py of the failing egg (with module name changed to foo to protect the innocent):

from setuptools import setup

setup(name='foo',
      description='Foo Module',
      packages=['foo'],
      entry_points = {
        'console_scripts': [
            'foo = foo.foo:main',
            ],
        },
      )

And here is the setup.py of the working egg:

from setuptools import setup

setup(name='bar',
      description='Bar Tool',
      packages=['bar', 'bar.hexes', 'bar.barlib'],
      entry_points = {
        'console_scripts': [
            'bar = bar.main:main',
            ],
       },
      )

Since I don't see any problems up until the moment I try to import the module, how can I debug this issue?

My leading suspect at the moment is the foo.foo reference in the console_scripts section of the setup.py. It sounds somewhat similar to the answer given by @joest in this problem.


Solution

  • The foo module could not be imported because there was no __init__.py in the egg. For reasons I still don't fully understand, our x64 build machines produce that file automatically during creation of the egg (You will see a Creating missing __init__.py for foo message), while the x86 build machines skip that step and produce a broken egg. Considering that we build Python itself and all the necessary modules as part of the process of building this egg, I don't see how the behavior could differ, but it does.

    I just created a blank __init__.py in the foo source directory, and now the eggs always build and import correctly.