if I'm writing a package in Python for distribution and I put some scripts to be regarded as executables in the scripts
of setup.py
, is there a standard way to make them not have the *.py extension? Is it sufficient to just make files that do not have the .py extension, or is anything extra needed? Will removing the .py from the filename that break any of the functionality associated with Python tools like setup.py
/distutils etc? Thanks.

- 39,603
- 20
- 94
- 123
-
3Have you tried it? I've done this sort of thing with `setuptools` and it worked just fine ... Give it a try -- install you module and see if `distutils` replaced the shebang with the shebang from your particular python. e.g. `#!/usr/bin/python27` or something of the like... – mgilson Jan 30 '13 at 03:17
3 Answers
The .py
extension is only necessary when you want to import the model AFAICT. Remember that pip
, easy_install
, and the like are simply executable files with the shebang at the top. The only OS that relies on file extensions for execution purposes is Windows.

- 58,213
- 10
- 98
- 113
If you need Windows compatibility then either don't remove the .py
extension or use setuptools' entry_points
option that automatically generates appropriate for the system script files e.g., to install pip.main()
function as a script, pip
specifies in setup.py
:
entry_points=dict(console_scripts=['pip=pip:main',
'pip-%s=pip:main' % sys.version[:3]]),
It makes sense to use entry_points
even if you don't need Windows compatibility because it generates the correct shebang for you that points to a specific python interpreter (where generic #! /usr/bin/env python
would be the wrong thing).

- 399,953
- 195
- 994
- 1,670
If the script is meant to be executed from the command line, the .py
extension doesn't actually do anything for you. The script will be executed using the Python interpreter under two circumstances:
- You explicity said to do so at the command line:
$ python nameofyourscript
- You explicity said to do so by including a shebang at the top of the script pointing to Python. The preferred version of that is
#!/usr/bin/env python
.
By including a shebang in each of your scripts, you can name the file anything you want.
Without doing one of these things, the script will be executed as a normal script meant for whatever shell you are using.

- 19,454
- 5
- 69
- 74