pythonpython-2.7package

First Python Package: Modules aren't loading


Background:

Apparently my question regarding the best ways to build an autoloader in Python was too broad (https://stackoverflow.com/questions/25678974/python-autoloader). Not sure why...

I had hoped to get a few code snippets to make my life easier with regards to choosing between importing modules or building a package. But not so much. So I decided to try to build a package of python functions meant to emulate PHP functions such str_replace.

I've named the package p2p and the directory structure is like this:

lib/p2p
  setup.py
  CHANGES.txt
  MANIFEST.IN
  README.txt
  /bin (empty)
  /docs (empty)
  /p2p
    __init__.py
    /str
      __init__.py
      str.py
    /time
      __init__.py
      time.py
    /json
      __init__.py
      json.py
    /utils
      __init__.py
      utils.py

The __init__.py are all empty.

In the Console:

Now when I build the package (python setup.py install) I get no errors. When I import p2p I get no errors. However, when I try to use p2p.time.timeID() I get:

AttributeError: 'module' object has no attribute 'timeID'

When I type p2p.time I get:

module 'p2p.time' from '/home/gabe/anaconda/lib/python2.7/site-packages/p2p/time/init.pyc'

I've Googled the attribute error and spent most of yesterday reading up on how to create a python package. What am I doing wrong? (Ultimately this appears like it would have been easier with simple modules and an autoloader. I'm not sure how to build an autoloader that will properly import the modules, due to relative path restrictions in Python, without just dumping my entire library directory structure into the path.)

lib/p2p/setup.py

from distutils.core import setup

setup(
    name='p2p',
    version='0.1.0',
    author='Me',
    author_email='me@example.com',
    packages=['p2p', 'p2p.test', 'p2p.str', 'p2p.time', 'p2p.json', 'p2p.utils'],
    package_dir={'p2p':'p2p', 'str':'p2p/str'},
    scripts=[],
    url='',
    license='LICENSE.txt',
    description='PHP to Python functions.',
    long_description=open('README.txt').read(),
    install_requires=[
        "",
    ],
)

lib/p2p/p2p/time/time.py

import time

# http://www.php2python.com/wiki/function.microtime/
def timeID():
    return time.time()

Notes:


Solution

  • p2p/time/time.py

    Well there you go. You want p2p.time.time.timeID instead. Or you could just put it in p2p/time/__init__.py instead.