重命名distutils中的脚本文件 [英] Rename script file in distutils

查看:66
本文介绍了重命名distutils中的脚本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a python script, myscript.py, which I wish to install using distutils:

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

如果我可以仅使用 myscript 而不是键入 myscript.py 来调用已安装的脚本, 。可以通过将文件重命名为 myscript 来实现,但随后许多编辑器等将不再理解它是Python文件。

I'd prefer if I could call the installed script using just myscript instead of typing myscript.py. This could be accomplished by renaming the file to just myscript but then a lot of editors etc. would no longer understand that it is a Python file.

是否可以保留旧名称 myscript.py ,但仍将文件安装为 myscript

Is there some way to keep the old name, myscript.py but still install the file as myscript?

推荐答案

您可能想看看为您自动执行此设置的setuptools;来自
http://pythonhosted.org/setuptools/setuptools.html#自动脚本创建

You might want to look at the setuptools that do this automatically for you; from http://pythonhosted.org/setuptools/setuptools.html#automatic-script-creation :


使用
distutils打包和安装脚本可能会有些尴尬。一方面,要使脚本的
文件名与Windows和POSIX平台上的本地约定匹配并不容易。
另外,当实际的 main是某个模块
中的函数时,通常必须为
main脚本创建一个单独的文件。即使在Python 2.4中,使用-m选项也仅适用于未安装在软件包中的
实际.py文件。

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修复了所有通过自动为您生成带有正确扩展名的
脚本来解决这些问题,在Windows上,
甚至会创建一个.exe文件,这样用户就不必更改
PATHEXT设置。使用此功能的方法是在安装脚本中定义入口
点,该点指示生成的
脚本应导入并运行什么功能。例如,要创建两个名为foo和bar的控制台
脚本和一个名为baz的GUI脚本,您可以执行
这样的操作:

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',
        ]
    }
)

这篇关于重命名distutils中的脚本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆