0

I am writing a command line tool with Python. It should be as easy as possible for Windows 10 / 11 users to install and use. My goal is to make the following procedure work:

  1. Install Python from the Microsoft Store
  2. Install my program using pip install "C:\path\to\myprogram.zip"
  3. Start my program using myprogram in Windows cmd

Currently, I have a setup.py like this:

from setuptools import setup

setup(
    name="myprogram",
    # image additional arguments here
    entry_points={"console_scripts": ["myprogram=myprogram.module:function"]},
)

Everything works great in virtual environments, but I don't want my users to necessarily deal with them. I understand that running the pip install ... command above installs the entry point (in my case) to C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\Scripts, which is obviously not in Windows %PATH%.

So here comes my question: Is it possible to either

  1. write the script to one of the Windows %PATH%s (for example C:\Users\myuser\AppData\Local\Microsoft\WindowsApps) or
  2. add C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\Scripts to Windows %PATH%?

I want the solution to work directly from the setup.py such that no additional installation step is necessary. I'm fully aware that there is only a small chance to find a solution that is not "hacky" in some way.


Workaround: As long as there is no solution to my question, I go with a __main__.py module and burden the users with the command python -m myprogram.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
d4tm4x
  • 458
  • 4
  • 14
  • This sound awfully complicated. Have you looked at PyInstaller or auto-py-to-exe? – Sebastian Mar 22 '23 at 11:06
  • You are absolutely right (this sounds awfully complicated). I have heard of py2exe, I'm just afraid that the security policies then require a Windows admin account? I have to assume that many of my users do not have one. I don't know PyInstaller, can similar problems occur there? – d4tm4x Mar 22 '23 at 11:10
  • Just try it. Besides an antivirus scan I have not encountered anything else. – Sebastian Mar 22 '23 at 11:11
  • Maybe this can help: https://stackoverflow.com/questions/36092388/adding-installed-pip-package-to-path-automatically – Code Master Mar 22 '23 at 11:31
  • @Sebastian: Thought about it during lunch. With these tools I need to implement and maintain some sort of build pipeline. My idea has some sort of charming pinguin style (thinking of `apt install myprog` and `myprog`). The zipfile I mentioned can be downloaded from Github directly. – d4tm4x Mar 22 '23 at 12:57
  • @CodeMaster: Very nice approach - unfortunately it only works on Python 2.x and explictly not on Win10 / Win11 (I looked up the paths). The script was removed from cpython couple of month ago... – d4tm4x Mar 22 '23 at 12:59
  • @CodeMaster Actually there was a Python 3 script as well - however, it was still removed with the argument being "outdated" ¯\ (ツ) /¯ – d4tm4x Mar 22 '23 at 14:47

1 Answers1

0

I would recommend you look into distributing your program using a zipapp-based solution, maybe pex or shiv. As long as a Python interpreter is installed, it should work without issues.

There are details regarding the handling of zipapp on Windows on its documentation page. I recommend making sure that the .pyz extension is configured properly to execute on double-click, but you might not be in a position to ensure this. I believe there are also other approaches to handle zipapps/.pyz files on Windows, which one you should use all depends on the details of the use case.


Another possible solution I would recommend to explore is pipx.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
  • Thank you for your suggestion. The double-click idea is not possible, since the command line call involves additional parameters. – d4tm4x Apr 03 '23 at 07:09
  • @d4tm4x Right, double-click is not necessary since the project is a CLI tool. Anyway, did you try something? pex or shiv? – sinoroc Apr 03 '23 at 07:46
  • I had a look, yes. However, compared to the workaround (see above), I need some build pipelines etc. which is an overhead I try to avoid until my program is hopefully used more and more... – d4tm4x Apr 03 '23 at 08:09
  • Your program has no dependencies? It uses only the standard library? – sinoroc Apr 03 '23 at 08:18
  • It uses pandas and plotly to generate visualizations of some kind of "log files" where the paths to such files are passed als command line arguments. – d4tm4x Apr 03 '23 at 13:45
  • If the program has dependencies then I recommend it should be either installed in a virtual environment (maybe with pipx) or distributed and used via a zipapp solution (maybe pex or shiv). – sinoroc Apr 03 '23 at 13:49