如何使用 setuptools 为 windows 命令行安装 python 脚本? [英] How to use setuptools to install a python script for windows command line?
问题描述
我一直在开发一个 python 命令行应用程序,我希望它可以从 windows 上的命令提示符以及 OSX 和 linux 上的终端运行.我的目标是制作一个 pip 模块,让我的应用程序在安装后可以在命令行上使用.
使用设置工具,我能够使用以下命令让脚本在 linux 和 OSX 上运行:
youtube-song-downloader <搜索查询>
运行后:
python setup.py install
通过pip安装后也可以直接工作
但是当我尝试在 windows 上使用相同的 setup.py 时,它无法识别该命令(可能是它没有添加到路径中)
我将如何通过 pip install 或 setup.py install 使我的脚本在 Windows 命令行上可用?
我的 setup.py:
#!/usr/bin/env python从 setuptools 导入设置设置(名称='youtube-song-downloader',版本='0.1',description='一个用于从 Youtube 搜索和下载歌曲的程序.',url='https://github.com/dszopa/youtube-song-downloader',作者 =丹尼尔·索帕",author_email='dszopa@iastate.edu',许可证='MIT',安装要求=['youtube_dl','google-api-python-client','pytest',],包=['ytsdl'],scripts=['bin/youtube-song-downloader'],include_package_data=真,zip_safe=假)
我的程序结构如下:
+ bin- youtube-song-downloader+ 测试+ ytsdl- 歌曲下载器.py- 设置.json- .gitignore- MANIFEST.in- 要求.txt- setup.py
如果您对我的代码有任何其他问题,我的 github 存储库是这里><小时>
编辑 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 install
which would also work directly after installing through pip
But 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 python
from setuptools import setup
setup(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='dszopa@iastate.edu',
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.py
If you have any further questions about my code my github repo is here
Edit 1
I added this to my setup.py
entrypoints={
'console_scripts': [
'youtube-song-downloader = ytsdl:main',
]
},
I also created a __main__.py
:
#!/usr/bin/env python
import sys
import os
import ytsdl
if __name__ == "__main__":
main()
def main():
#script from bin/youtube-song-downloader
and also added __main__
to my __init__
with
from .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 thesetup
call in yoursetup.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 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!