gpt4 book ai didi

python - 在 distutils 中重命名脚本文件

转载 作者:IT老高 更新时间:2023-10-28 20:47:00 26 4
gpt4 key购买 nike

我有一个 python 脚本 myscript.py,我希望使用 distutils 安装它:

from distutils.core import setup
setup(..., scripts=['myscript.py'], ...)

如果我可以只使用 myscript 而不是键入 myscript.py 来调用已安装的脚本,我会更喜欢。这可以通过将文件重命名为 myscript 来完成,但是很多编辑器等将不再理解它是 Python 文件。

有什么方法可以保留旧名称 myscript.py 但仍将文件安装为 myscript

最佳答案

您可能想查看自动为您执行此操作的设置工具;从 http://pythonhosted.org/setuptools/setuptools.html#automatic-script-creation :

Packaging and installing scripts can be a bit awkward with the distutils. For one thing, there’s no easy way to have a script’s filename match local conventions on both Windows and POSIX platforms. For another, you often have to create a separate file just for the “main” script, when your actual “main” is a function in a module somewhere. And even in Python 2.4, using the -m option only works for actual .py files that aren’t installed in a package.

setuptools fixes all of these problems by automatically generating scripts for you with the correct extension, and on Windows it will even create an .exe file so that users don’t have to change their PATHEXT settings. The way to use this feature is to define “entry points” in your setup script that indicate what function the generated script should import and run. For example, to create two console scripts called foo and bar, and a GUI script called baz, you might do something like this:

setup(
# other arguments here...
entry_points={
'console_scripts': [
'foo = my_package.some_module:main_func',
'bar = other_module:some_func',
],
'gui_scripts': [
'baz = my_package_gui:start_func',
]
}
)

关于python - 在 distutils 中重命名脚本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4359231/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com