Posts: 233
Threads: 0
Joined: Jun 2019
Comments on this code:
* The while loop is pointless.
* There's an import for subprocess missing.
* And even if you import, our line 5 while raise an error (hint: check the spelling).
Except, it is questionable whether you want to share code with other which automagically installs software on the user's computer without asking the user for permission. Although you most likely have good intentions, it's basically the same thing malware does.
More alternative ways to share Python applications without the need to have Python installed on the target machine are zipapp and Nuitka.
But as installing Python is easy, it can be installed on Windows via the Microsoft Store, this may be the easier way.
Regards, noisefloor
Posts: 4,905
Threads: 79
Joined: Jan 2018
Oct-12-2025, 09:54 AM
(This post was last modified: Oct-12-2025, 09:55 AM by Gribouillis.)
(Oct-12-2025, 09:28 AM)kucingkembar Wrote: why this code willl not work?
i tried it sometime and work without problem. This will probably work better
try:
import thepackage
except ModuleNotFoundError:
import sys
import subprocess
subprocess.check_output([sys.executable, '-m', 'pip', 'install', thepackage])
import thepackageHowever I'm sure nothing can beat Snippsat's code.
« We can solve any problem by introducing an extra level of indirection »
Posts: 167
Threads: 49
Joined: Nov 2021
@ noisefloor
why The while loop is pointless? (lets assume I do not the typo)
as for subprocess and #only example , I did the typo on purpose, so you will guessing what concept I like to do
"installing Python is easy", for expert or Tech Users, I agree,
but I remember, I tried to give the code to Non-Tech Users,
I expect they do not know what py file do, or bat file do
Posts: 7,432
Threads: 125
Joined: Sep 2016
Oct-12-2025, 12:40 PM
(This post was last modified: Oct-12-2025, 12:40 PM by snippsat.)
This is what i would have done if share ML code(that always install lot of packages),
with a none Tech User,to make it as easy as possible,if looking away from just a .exe file(thar can be problematic with ML code).
I have tested this on clean Pc with no Python or uv installed
So app.py and uv.exe in a folder that you give to user.
Then the only instruction you give them is to run in folder from cmd(command line)
uv run app.py Here is a run on this clean OS no Python.
C:\code\ml_app>uv run app.py
Install Python 3.11.4 .....
Installed 12 packages in 14.86s
Torch: 2.8.0+cpu
Torchvision: 0.23.0+cpu
Sample tensor: tensor([[0.7411, 0.5219, 0.6529],
[0.0066, 0.8266, 0.4761]])Just change in code,so it don't dowload Python 3.14 that may not have wheel for all ML packages yet
requires-python = ">=3.10, <3.12" In this example it downloaded and used Python 3.11.4.
Posts: 1,306
Threads: 153
Joined: Jul 2017
Quote:A. as I said it for Non-Tech Users, I think everybody have acknowledged that linux for Tech Users, mac for People with money, and the rest is windows for common people, I think your method will not work for people that have problem with wizard instalation
I would disagree with this assessment.
Linux, like Python, is free. The source code for Linux is freely available. Anyone can suggest changes. Linux is a community project, the result of cooperation of people around the world.
Windows was never free. Windows source code is a company secret. Windows + Intel, Wintel, have such a grip on computing worldwide that it is very difficult to buy a computer without buying Windows too. That is why Windows is so widespread
Mac os is Unix based, the same as Linux and Android. Mac / Apple computers are more expensive, but very good quality.
If anyone wants to install Python and learn some programming, that person will need some basic knowledge of computers and operating systems.
Go to your friend, download Python from python.org, put your home-made module in a folder and show your friend 1 or 2 or 3 times how to make it available to the Python interpreter.
Almost all Python questions have an answer out there on the internet.
Posts: 167
Threads: 49
Joined: Nov 2021
@ Pedroski55 i wont argue about OS,
i talking about skill, each person has their own strengths and weaknesses,
me for example, I can code video game, but I have difficulty creating story, arts, music,
Posts: 233
Threads: 0
Joined: Jun 2019
(Oct-12-2025, 10:03 AM)kucingkembar Wrote: why The while loop is pointless? (lets assume I do not the typo) Because the loop never loops in your example. Thus it is pointless. The shown code works exactly the same way without a while.
Regards, noisefloor
|