Search code examples
pythonpip

pip upload script or module


I am sorry in advance if this is a duplicate of another question (and this is my first question here :) )

I would like to know if we can upload scripts to pip rather than packages. I am currently writing a code and would like to make it available to the community through pip but I am not sure if this is possible...

When I say package I mean something that we can call inside another code with

import blabla

And script something that we can call from the terminal like any other program.


Solution

  • You can upload scripts, but you have to package them if you want to make it available to the community through a pip install. That means you'll need to write a setup.py file, too.

    # my_script.py
    def main():
        ...
    
    if __name__ == '__main__':
        main()
    

    In the setup file:

    # setup.py
    from setuptools import setup
    
    setup(
        ...
        entry_points={
            'console_scripts': ['my_script=my_script:main'],
        },
    )
    

    There is other stuff you'll need to learn about to upload your script to the Python package index. Check out a packaging guide, such as PyPI Quick and Dirty.