xtian
xtian

Reputation: 2947

Click module Setuptools example didn't work out of the box

Click setuptools has two examples. I can get the first example to work, but not the second.

I understand there are still two files, but now yourscript.py is stored in a sub-directory scripts:

# yourscript.py
import click

@click.command()
def cli():
    """Example script."""
    click.echo('Hello World!')

Setup.py then supplys an argument find_packages() and entry_points uses a dot syntax to describe the location of yourscript.py

# setup.py
from setuptools import setup, find_packages
setup(
    name='yourpackage',
    version='0.1',
    packages=find_packages(),
    include_package_data=True,
    install_requires=[
        'Click',
    ],
    entry_points='''
        [console_scripts]
        yourscript=yourpackage.scripts.yourscript:cli
    ''',
)

I followed the example using virtualenv:

(clickenv) > pip install -e .  
Obtaining file:///home/...ClickSiteExample/yourscript
Requirement already satisfied: Click in /home/.../clickenv/lib/python3.6/site-packages (from yourpackage==0.1) (6.7)
Installing collected packages: yourpackage
  Running setup.py develop for yourpackage
Successfully installed yourpackage

Then here's the call to the script:

(clickenv) > yourscript        
    [...]
ModuleNotFoundError: No module named 'yourpackage'

Something is missing in this error report, because the package is also shown as installed:

(clickenv) [130] > pip list
Package     Version Location                                                                            
----------- ------- --------------------------------------------------
click       6.7     
pip         10.0.0  
setuptools  39.0.1  
wheel       0.31.0  
yourpackage 0.1     /home/.../ClickSiteExample/yourpackage <<CORRECTED

This should be simple, so what's missing from the example?


UPDATE. I've recreated the directory minus the two unmentioned files, main.py and utils.py. I understand the __init__.py files are required but can be empty, and I've included those:

(clickenv) > tree 
.                                                                                
└── yourpackage  <<CORRECTED
    ├── __init__.py
    ├── scripts
    │   ├── __init__.py
    │   └── yourscript.py
    ├── setup.py
    └── yourpackage.egg-info
        ├── dependency_links.txt
        ├── entry_points.txt
        ├── PKG-INFO
        ├── requires.txt
        ├── SOURCES.txt
        └── top_level.txt

Upvotes: 4

Views: 1957

Answers (1)

phd
phd

Reputation: 94453

This is the directory structure that works:

yourpackage <- Any top-level directory will do
|-- setup.py
|-- yourpackage
|   |-- __init__.py
|   `-- scripts
|       |-- __init__.py
|       |-- yourscript.py

After pip install -e .:

`-- yourpackage.egg-info
    |-- dependency_links.txt
    |-- entry_points.txt
    |-- PKG-INFO
    |-- requires.txt
    |-- SOURCES.txt
    `-- top_level.txt

The entry point script works:

$ yourscript
Hello World!

Upvotes: 3

Related Questions