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

时间秒杀一切 提交于 2020-05-28 03:54:18

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/61970508/how-to-create-a-exe-similar-to-pip-exe-jupyter-exe-etc-from-c-python37-scri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!