Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename script file in distutils

Tags:

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 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.

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

like image 769
pafcu Avatar asked Dec 05 '10 14:12

pafcu


2 Answers

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',         ]     } ) 
like image 150
cweiske Avatar answered Jan 12 '23 06:01

cweiske


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'],     ... ) 
like image 25
MFreck Avatar answered Jan 12 '23 06:01

MFreck