Python Egg无法在我的项目中加载子模块

时间:2017-09-12 10:22:22

标签: python package setup.py egg

我正在尝试使用cement(builtoncement)库

为我的python项目构建创建一个egg包

我的项目结构如下所示

connector
    README.md
    requirements.txt
    setup.py
    __main.py__
    __init.py__
    lib
        __init__.py
        handlers
               __init__.py
                logging
                       __init__.py
                        LogStashHandler.py

__main.py__的内容是

import sys
import os
from cement.core.foundation import CementApp
from lib.handlers.logging.LogStashHandler import LogStashHandler
……


class ConnectorApp(CementApp):


def main():
    log = None
    with ConnectorApp() as app:
        log = app.log



if __name__ == '__main__':
    main()

setup.py的内容是

#!/usr/bin/env python

import uuid
from setuptools import setup
import os
from pip.req import parse_requirements

def read(fname):
    return open(os.path.join(os.path.dirname(__file__), fname)).read()

BASE_DIR = os.path.dirname(os.path.realpath(__file__))
reqs_file = os.path.join(BASE_DIR, 'requirements.txt')
install_reqs = parse_requirements(reqs_file, session=uuid.uuid1())

setup(
    name="connectors",
    version="0.1",
    author="Pradeep Mishra",
    install_requires=["argparse", "requests", "pyyaml", "datetime", "cement"],
    packages=['lib'],
    long_description=read('README.md'),
    data_files=[ ('', ['__main__.py', ])],
    classifiers=[     
        "Programming Language :: Python :: 2.7"
    ],
)

我正在使用以下命令安装程序

python setup.py build
python setup.py sdist
python setup.py bdist_egg

当我尝试通过进入dist文件夹来运行程序时,我收到以下错误:

(venv) Pradeeps-MacBook-Pro:dist pradeepmishra$ python connectors-0.1-py2.7.egg 
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "connectors-0.1-py2.7.egg/__main__.py", line 4, in <module>
ImportError: No module named handlers.logging.LogStashHandler

请帮忙。如何告诉egg builder找到lib模块中的模块和子模块

1 个答案:

答案 0 :(得分:1)

我自己找到了。 我没有在包中指定顶级包,而是传递了函数find_packages(),它就像魅力一样。

我的设置功能现在看起来像这样     #!/ usr / bin / env python

Condition