Shaurya Chaudhuri
Shaurya Chaudhuri

Reputation: 3842

How to write a correct setup.py

I was trying to write a setup.py for a small chat application I created.

Here is the setup.py code:-

#!/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='[email protected]',
    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.

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.

Upvotes: 4

Views: 1552

Answers (1)

kchomski
kchomski

Reputation: 3010

I've just cloned your project and it has wrong folder structre - you should put files related to your application into another folder (e.g. chatery), not in the same one where other distribution-related files reside.
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:

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):

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: enter image description here

Upvotes: 4

Related Questions