Packaging a Python library with an executable

前端 未结 1 1533
挽巷
挽巷 2020-12-19 02:59

I just finished a module and want to package it. I\'ve read the documentation and this question packaging a python application but I am not sure about how to proceed when I

相关标签:
1条回答
  • 2020-12-19 03:14

    Generally, you only distribute python packages as modules when the entire project fits in a single module file. If your project is more complex than that, it's usually best to structure your project as a package with an __init__.py file. Here is what your project would look like converted to a package

    Project/
    |-- README
    |-- requirement.txt
    |-- setup.py
    |-- scripts/
    |   |-- driver.py
    |-- driver/
    |   |-- __init__.py
    |   |-- module_1
    |   |   |-- __init__.py
    |   |   |-- class_1.py
    |   |   |-- class_2.py
    |   |-- module_2
    |   |-- |-- __init__.py
    |   |-- |-- class_1.py
    |   |-- |-- class_2.py
    

    I renamed your run.py to scripts/driver.py and the code you previously had in driver.py is now driver/__init__.py.

    Your setup.py should look like this

    from setuptools import setup. find_packages
    
    setup(
        name='driver',
        version='1.0',
        packages=find_packages(),
        scripts=['scripts/driver.py'],
    )
    

    This will copy scripts/driver.py to the python Scripts directory. I renamed run.py to driver.py since run is pretty generic and you want your script names to be unique since all python packages share the same scripts location.

    Alternatively, you could use the console_scripts entry point. In this case, you wouldn't have a separate scripts/driver.py script. Instead, you would just have a function inside your package. In this case, you could move all the code from scripts/driver.py into driver/command_line.py and put it inside a function called main(). Then change your setup.py to this

    setup(
        name='driver',
        version='1.0',
        packages=find_packages(),
        entry_points = {
            'console_scripts': ['driver=driver.command_line:main'],
        }
    )
    

    Also, you should read this docs page on python packaging. It covers the basics and a lot of the common use cases.

    0 讨论(0)
提交回复
热议问题