1

Some python packages such as flask can be used as shell script after installation via pip install. My question is how to create them?

A minimal package can be written as below, and where to add codes?

.
├── library_name
│   ├── __init__.py
│   └── Foo.py
└── setup.py

Thank you!

chitakuma
  • 329
  • 4
  • 11

1 Answers1

2

Use the entrypoints in your setup.py

from setuptools import setup, find_packages

setup(
    name='Foo',
    version='1.0',
    packages=find_packages(),
    url='https://github.com/Bar/Foo.git',
    install_requires=[

    ],
    license='',
    classifiers=[
        'Development Status :: 3 - Alpha',

    ],
    keywords='foo bar',
    author='Mr Foo',

    entry_points={
        'console_scripts': [
            'foo = library_name.Foo:main',
        ],
    },
)

This way when you python setup.py install you can call foo from shell and it will execute main in Foo.py

Note that you need to change library_name, foo, Foo and main in case they have different names in your code.

E.Serra
  • 1,495
  • 11
  • 14
  • May be we also need to add PATH: https://stackoverflow.com/questions/46973667/python-console-scripts-doesnt-work-when-pip-install-user – chitakuma Mar 18 '19 at 09:38
  • no that is bad practice, use the virtualenv to modify PATH, don't mess with it manually – E.Serra Mar 18 '19 at 10:21
  • What I mean is don't install ANYTHING, EVER if you are not in a virtualenvironment. That will also solve your path problem in this case. – E.Serra Mar 18 '19 at 10:24