Domanda

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.

È stato utile?

Soluzione 2

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

Altri suggerimenti

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.

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top