简体   繁体   English

如何编写正确的setup.py

[英]How to write a correct setup.py

I was trying to write a setup.py for a small chat application I created. 我正在尝试为我创建的小型聊天应用程序编写setup.py。

Here is the setup.py code:- 这是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
)

I took reference from the httpstat project which is small as well, but apparrently that works and my setup.py does not. 我从httpstat项目中获取了一个很小的参考,但是很明显可行,而我的setup.py却没有。

It isn't able to find the file that it is supposed to load. 它无法找到它应该加载的文件。 I get the following errors. 我收到以下错误。 When I write chatery on console. 当我在控制台上写字时。

    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

Not sure what I am doing wrong here. 不知道我在这里做错了什么。

I've just cloned your project and it has wrong folder structre - you should put files related to your application into another folder (eg chatery ), not in the same one where other distribution-related files reside. 我刚刚克隆了你的项目并且它有错误的文件夹结构 - 你应该将与你的应用程序相关的文件放到另一个文件夹(例如chatery ),而不是放在其他分发相关文件所在的文件夹中。
So instead of having this: 所以不要这样:

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

you should have something like this: 你应该有这样的东西:

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

In your setup.py you need to slightly modify entry-points argument to: 在你的setup.py你需要稍微修改entry-points参数:

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

You also need to create MANIFEST.in file to include assets folder (you might want to add database folder as well if it's needed by your application): 您还需要创建MANIFEST.in文件以包含assets文件夹(如果应用程序需要,您可能还需要添加database文件夹):

recursive-include chatery/assets *

Now you can install and run your application: 现在您可以安装并运行您的应用程序:

~$ 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

And when I enter http://127.0.0.1:9000 in my browser, I get: 当我在浏览器中输入http://127.0.0.1:9000时,我得到: 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM