如何编写正确的setup.py

时间:2017-08-19 10:02:49

标签: python python-3.x setup.py

我正在尝试为我创建的small chat application编写一个setup.py。

以下是 setup.py 代码: -

#!/usr/bin/env python
import os
from setuptools import setup, find_packages
from os import environ as env
import subprocess

from pip.req import parse_requirements

requirements = [str(req.req) for req in parse_requirements('requirements.txt', session=False)]

try:
    VERSION = subprocess.check_output(['git', 'describe', '--tags']).strip()
except subprocess.CalledProcessError:
    VERSION = '0.dev'

setup(
    name='chatery',
    version=VERSION,
    description="Lightweight Chat application"
                " - with Twitter Support",
    long_description=open('README.md').read(),
    author="Shaurya-Xoxzo",
    author_email='shauryadeepc@hotmail.com',
    url='http://www.xoxzo.com',
    license='MIT',
    install_requires=requirements,
    packages=find_packages(),
    include_package_data=True,
    entry_points={
        'console_scripts': [
            'chatery = chatery:main',
        ],
    },
    zip_safe=False
)

我从httpstat项目中获取了参考,该项目虽然很小,但显然可行,而我的setup.py则没有。

无法找到应该加载的文件。我收到以下错误。 当我在控制台上写字时。

    Traceback (most recent call last):
  File "/usr/local/bin/chatery", line 9, in <module>
    load_entry_point('chatery==0.dev0', 'console_scripts', 'chatery')()
  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 542, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2569, in load_entry_point
    return ep.load()
  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2229, in load
    return self.resolve()
  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2235, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
ImportError: No module named chatery

不确定我在这里做错了什么。

1 个答案:

答案 0 :(得分:4)

我刚刚克隆了您的项目并且文件夹结构错误 - 您应该将与您的应用程序相关的文件放入另一个文件夹(例如chatery),而不是放在其他与分发相关的文件中驻留。
所以不要这样:

app.py
assets/
Caddyfile
constants.py
database/
dbutils/
install.sh
README.md
requirements.txt
run.sh
setup.py
tests/
utils.py

你应该有这样的东西:

Caddyfile
chatery/
    app.py
    assets/
    constants.py
    database/
    dbutils/
    __init__.py
    utils.py
install.sh
README.md
requirements.txt
run.sh
setup.py
tests/

setup.py中,您需要稍微修改entry-points参数:

entry_points={
    'console_scripts': [
        'chatery=chatery.app:main',
    ],
}

您还需要创建MANIFEST.in文件以包含assets文件夹(如果您的应用需要,您可能还想添加database文件夹):

recursive-include chatery/assets *

现在您可以安装并运行您的应用程序:

~$ python setup.py install
   # many long lines
~$ chatery
[2017-08-19 14:43:21,994] INFO Using epoll
[19/Aug/2017:14:43:21] ENGINE Listening for SIGHUP.
[19/Aug/2017:14:43:21] ENGINE Listening for SIGTERM.
[19/Aug/2017:14:43:21] ENGINE Listening for SIGUSR1.
[19/Aug/2017:14:43:21] ENGINE Bus STARTING
[19/Aug/2017:14:43:21] ENGINE Starting WebSocket processing
[19/Aug/2017:14:43:21] ENGINE Started monitor thread '_TimeoutMonitor'.
[19/Aug/2017:14:43:21] ENGINE Started monitor thread 'Autoreloader'.
[19/Aug/2017:14:43:22] ENGINE Serving on http://127.0.0.1:9000
[19/Aug/2017:14:43:22] ENGINE Bus STARTED

当我在浏览器中输入http://127.0.0.1:9000时,我得到: enter image description here