Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a .exe similar to pip.exe, jupyter.exe, etc. from C:\Python37\Scripts?

In order to make pip or jupyter available from command-line on Windows no matter the current working directory with just pip ... or jupyter ..., Python on Windows seems to use this method:

  • put C:\Python37\Scripts in the PATH
  • create a small 100KB .exe file C:\Python37\Scripts\pip.exe or jupyter.exe that probably does not much more than calling a Python script with the interpreter (I guess?)

Then doing pip ... or jupyter ... in command-line works, no matter the current directory.

Question: how can I create a similar 100KB mycommand.exe that I could put in a directory which is in the PATH, so that I could do mycommand from anywhere in command-line?


Note: I'm not speaking about pyinstaller, cxfreeze, etc. (that I already know and have used before); I don't think these C:\Python37\Scripts\ exe files use this.

like image 929
Basj Avatar asked Sep 14 '25 11:09

Basj


1 Answers

Assuming you are using setuptools to package this application, you need to create a console_scripts entry point as documented here:

  • https://packaging.python.org/guides/distributing-packages-using-setuptools/#console-scripts

For a single top-level module it could look like the following:

setup.py

#!/usr/bin/env python3

import setuptools

setuptools.setup(
    py_modules=['my_top_level_module'],
    entry_points={
        'console_scripts': [
            'mycommand = my_top_level_module:my_main_function',
        ],
    },
    # ...
    name='MyProject',
    version='1.0.0',
)

my_top_level_module.py

#!/usr/bin/env python3

def my_main_function():
    print("Hi!")

if __name__ == '__main__':
    my_main_function()

And then install it with a command such as:

path/to/pythonX.Y -m pip install path/to/MyProject
like image 174
sinoroc Avatar answered Sep 17 '25 02:09

sinoroc