编辑 1我将此添加到我的 setup.py 入口点={控制台脚本":['youtube-song-downloader = ytsdl:main',]},我还创建了一个__main__.py:#!/usr/bin/env python导入系统导入操作系统导入 ytsdl如果 __name__ == "__main__":主要的()定义主():#script 来自 bin/youtube-song-downloader并且还添加了 __main__ 到我的 __init__ 中from .ytsdl import __main__我不确定我是否朝着正确的方向前进,程序已编译但没有使用相同的命令 解决方案 在开发时,请确保您有一个包含python"的shebang;(#!/usr/bin/env python) 在所有脚本文件中的名称中,这些文件应该稍后由用户直接执行,甚至以后目标平台上的路径不同(https://docs.python.org/3/distutils/setupscript.html#installing-scripts)将所有用户可调用的脚本放入 setup 调用的 scripts 参数中,如您所示(尽管我会不要省略 .py 扩展名).一旦使用 pip install 安装在目标平台上,目标 shebang 就会被一个合适的替换,并且脚本被放置在找到它们的路径中.如果用户正确设置了 Python,她应该能够使用它们而无需预先添加 python.例如,对于 Windows,Python 启动器 (py.exe) 应该与 .py 文件相关联,这在 Python 安装过程中是可选的.要省略 .py 扩展名,.py 可以在 PATHEXT 环境变量中.I have been working on a python command line application that I want to be runnable from command prompt on windows as well as the terminal in OSX and on linux.My goal is to make a pip module that allows my application to be useable on the command line once installed.Using setup tools I was able to get the script to run on both linux and OSX with the command:youtube-song-downloader <search query>after running:python setup.py installwhich would also work directly after installing through pipBut when I try to use the same setup.py on windows, it is unable to recognize the command (likely that it is not added to the path)How would I go about making my script useable on the windows command line from a pip install or setup.py install?My setup.py:#!/usr/bin/env pythonfrom setuptools import setupsetup(name='youtube-song-downloader', version='0.1', description='A program used to search and download songs from Youtube.', url='https://github.com/dszopa/youtube-song-downloader', author='Daniel Szopa', author_email='[email protected]', license='MIT', install_requires=[ 'youtube_dl', 'google-api-python-client', 'pytest', ], packages=['ytsdl'], scripts=['bin/youtube-song-downloader'], include_package_data=True, zip_safe=False)My program structure is as follows:+ bin - youtube-song-downloader+ tests+ ytsdl - songDownloader.py - settings.json- .gitignore- MANIFEST.in- requirements.txt- setup.pyIf you have any further questions about my code my github repo is hereEdit 1I added this to my setup.py entrypoints={ 'console_scripts': [ 'youtube-song-downloader = ytsdl:main', ] },I also created a __main__.py:#!/usr/bin/env pythonimport sysimport osimport ytsdlif __name__ == "__main__": main()def main(): #script from bin/youtube-song-downloaderand also added __main__ to my __init__ withfrom .ytsdl import __main__Im pretty unsure if I'm headed in the right direction, the program compiled but had no luck with the same command 解决方案When developing, make sure you have a shebang containing "python" (#!/usr/bin/env python) in the name in all script files that should be later directly executed by the user, even later the path is different on target platforms (https://docs.python.org/3/distutils/setupscript.html#installing-scripts)Put all user-callable scripts to the scripts argument of the setup call in your setup.py as you showed (although I would not omit the .py extension).Once installed on the target platform with pip install the target shebang is replaced by a suitable one and the scripts are put in a path where they are found.If the user has setup Python correctly, she should be able to use them without prepending python.For Windows, for example, the Python Launcher (py.exe) should be associated with .py files, that's optionally offered during Python setup. To omit the .py extension, .py can be in the PATHEXT environment variable. 这篇关于如何使用 setuptools 为 windows 命令行安装 python 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-03 11:43