开发者

Rename script file in distutils

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

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

I'd prefer if I could call the installed script using just m开发者_Python百科yscript 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.

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


You might want to look at the setuptools that do this automatically for you; from 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',
        ]
    }
)


You could always do something like this (in setup.py):

import os
import shutil

if not os.path.exists('scripts'):
    os.makedirs('scripts')
shutil.copyfile('myscript.py', 'scripts/myscript')

setup(...
    scripts=['scripts/myscript'],
    ...
)


This is the cleanest solution I have found so far. MFrecks answer causes problems, when creating a source distribution or executing a command other than installing.

import distutils.command.install_scripts
import shutil

class my_install(distutils.command.install_scripts.install_scripts):
    def run(self):
        distutils.command.install_scripts.install_scripts.run(self)
        for script in self.get_outputs():
            if script.endswith(".py"):
                shutil.move(script, script[:-3])

setup(..., cmdclass = {"install_scripts": my_install}, ...)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜