Distributing Python scripts without .py extension
Asked Answered
W

3

7

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.

Widener answered 30/1, 2013 at 3:12 Comment(1)
Have 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...Anaplastic
S
1

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

Searchlight answered 30/1, 2013 at 4:48 Comment(0)
G
2

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.

Generative answered 30/1, 2013 at 3:28 Comment(0)
S
1

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

Searchlight answered 30/1, 2013 at 4:48 Comment(0)
H
0

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:

  1. You explicity said to do so at the command line: $ python nameofyourscript
  2. 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.

Hydrogeology answered 30/1, 2013 at 4:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.