2

How can I configure setup.py to install scripts with a .py prefix on Windows platforms but without the prefix on all others? The problem I'm trying to solve is that Windows requires a .py extension to recognize and execute Python scripts.

I have a package set up like the following:

MyPackage
├── CHANGES
├── ...
├── scripts
│   └── myprogram
├── setup.py
└── mypackage
    ├── __init__.py
    ├── ...
    └── myprogram.py

In my setup.py file I declare scripts/myprogram as an installable script with

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from setuptools import setup

...

setup(
    name='MyPackage',
    ...
    packages=['mypackage'],
    scripts=['scripts/myprogram'],
    ...
)

The myprogram script is just a thin wrapper that simply calls mypackage.myprogram.main():

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from mypackage import myprogram
myprogram.main()

On *nix platforms, this installs myprogram as an executable by the name of myprogram, which is what I desire, but on Windows, it also installs as myprogram in the C:\PythonXX\Scripts directory, and is thus not recognized on the command line. How can I get the setup script to install myprogram as myprogram.py on Windows, so that Windows recognizes the file type to make it executable?

gotgenes
  • 38,661
  • 28
  • 100
  • 128

1 Answers1

0

I found the answer: a keyword argument to setup() called called entry_points with a dictionary containing 'console_scripts', as described in this answer to a related question.

The new setup call looks like this:

 setup(
    name='MyPackage',
    ...
    packages=['mypackage'],
    entry_points={
        'console_scripts': [
            'myprogram = mypackage.myprogram:main'
        ]
    }
    ...
)

After making those changes I then deleted the scripts directory and the myprogram file from it.

Now when running the installer, distribute creates two files under C:\PythonXX\Scripts: myprogram-script.py and myprogram.exe. As a result, the user can now enter myprogram on the command line of either *nix or Windows platforms and always have it run the same program. Very handy!

Community
  • 1
  • 1
gotgenes
  • 38,661
  • 28
  • 100
  • 128