jwir3
jwir3

Reputation: 6209

"No module named 'core'" after rename of modules

I have the following directory structure:

src/
  |- setup.py
  |- prettylogger
    |- __init__.py
    |- core.py
  |- transgression
    |- __init__.py
    |- core.py

The problem I'm having is that it doesn't quite setup correctly. What I want it to do is to install two packages - prettylogger and transgression so that prettylogger can be imported independently of transgression, and vice versa, using from prettylogger.core import PrettyLogger (class PrettyLogger is contained within prettylogger/core.py.

I've tried the setup.py file following this post, but with little success. It seems to install a .egg file into /Library/Python/2.7/site-packages (I'm on a mac) for prettylogger and transgression, but when I run: transgression at the command line, I get:

  File "/usr/local/bin/transgression", line 8, in <module>
    load_entry_point('transgression==0.0.1', 'console_scripts', 'transgression')()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 318, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 2221, in load_entry_point
    return ep.load()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 1954, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
  File "build/bdist.macosx-10.9-intel/egg/transgression/core.py", line 2, in <module>
  File "build/bdist.macosx-10.9-intel/egg/configurator/core.py", line 16, in <module>
ImportError: No module named core

I have a test suite in transgression/transgression-test.py, and when I cd to that directory and run it, I get:

Traceback (most recent call last):
  File "build/bdist.macosx-10.9-intel/egg/transgression/core.py", line 16, in <module>
    from prettylogger.core import PrettyLogger
ImportError: No module named core

So, it seems to be that it can't find this module. I just renamed the module from prettylogger.py to core.py, because I wanted to be able to use: from prettylogger.core import PrettyLogger instead of from prettylogger.prettylogger import PrettyLogger.

Setup.py:

from setuptools import setup                                                    
import os                                                                       

setup(name='prettylogger',                                                      
      version='0.0.1',                                                          
      description='A logging utility package designed for command line use',    
      py_modules=['prettylogger.core'],
      # Note: I also have tried packages=['prettylogger'], here                                        
      install_requires=['ansicolors'] 
)                                                                               

setup(name='transgression',                                                     
      version='0.0.1',                                                          
      description='Generic binary regression finding utility',
      packages=['transgression'],                                               
      entry_points={ 'console_scripts': [                                       
        'transgression = transgression.core:main'] },                           
      install_requires=['prettylogger']                         
)                                                                               

Upvotes: 0

Views: 4613

Answers (2)

suhailvs
suhailvs

Reputation: 21730

Files named __init__.py are used to mark directories on disk as a Python package directories.

If you remove the __init__.py file, Python will no longer look for submodules inside that directory, so attempts to import the module will fail.

If you have lots of folders, then create the empty __init__.py file in each folder. for eg:

src/
  |- __init__.py
  |- setup.py
  |- prettylogger
    |- __init__.py
    |- core.py
  |- transgression
    |- __init__.py
    |- core.py

Upvotes: 0

marshall.ward
marshall.ward

Reputation: 7198

Do you have an __init__.py file in your package directories? Python requires this file in order to recogize the directory and its contents to be packages (although I think this may have changed in the newer Python 3.x versions).

The __init__.py file can be empty, it doesn't need any additional content.

Upvotes: 3

Related Questions