4
votes

I've been trying to package my project with setup.py, but I ran into a snag.

My file structure is as follows:

root/
  mypackage/
     __init__.py
     mysubmodule1/
        __init__.py
     mysubmodule2/
        __init__.py

I'm using the following configuration in my setup.py,

from setuptools import setup, find_packages
# To use a consistent encoding
from os import path
import glob

here = path.abspath(path.dirname(__file__))

print(find_packages(exclude=['docs', 'tests*']))
setup(
    name='mypackage',
    packages=find_packages(exclude=['contrib', 'docs', 'tests*']),
    install_requires=[...],
    scripts=[...],
)

I build the package with python setup.py install inside a virtualenv, the debug line I printed showed that find_packages located all my pacakges.

['mypackage', 'mypackage.submodule1', 'mypackage.submodule2']

When I import my package, I attempted to import mypackage.submodule1.class, but this threw a module not found exception. I checked that all my modules are in the output egg in the virtualenv site-packages, and that I can import my root package.

The output of the dir(mypackage) is as follows:

Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mypackage
>>> dir(mypackage)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__']

Am I missing something? All the opensource projects I referenced follow this pattern. Thanks

1

1 Answers

0
votes

Found a work around. It seems that just running setup.py is not packaging all my files and making it visible.

So instead I ran my local setup in 2 stages to get all the files on my python environment.

This first command will create a distribution with all the files in a dist folder.

$ python setup.py sdist

$ tree dist
dist
├── TrainingDataAgent-0.0.1-py2.7.egg
└── TrainingDataAgent-0.0.1.tar.gz

$ pip install  dist/TrainingDataAgent-0.0.1.tar.gz  

This allowed my calling program to correctly locate all the files of my python package.