Python Forum
sharing ready to use code to Non-Tech Users
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sharing ready to use code to Non-Tech Users
#1
Sorry for my bad English

i want to share my ready to use code to my friend that not a programmer.
I tried :
while True:
  try:
    import thepackage
  except:
    subproces("pip install thepackage")#only example
  else:
    break
but I wonder, what if my friend / the user cannot install the python correctly,
some setting that include "add path" or else may make the python not work properly.

do you have any way or tips to solve this problem?
Reply
#2
The code you have will not work.
To give a couple tips,first one pyinstaller.
Then you give user a .exe file,then no Python is required alt all.
A GUI version auto-py-to-exe.

The new uv package manager make it easier.
User have to install uv eg on Windows.
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
Then can eg give code to user like this.
example.py
# /// script
# requires-python = ">=3.13"
# dependencies = [
#     "requests<3",
#     "rich",
# ]
# ///

from rich import print
import requests

def main():
    print("[bold cyan]Hello from a package![/]")
    print(f'IP: {requests.get("https://api.ipify.org").text}')

if __name__ == "__main__":
    main()
User run with uv run example.py
λ uv run example.py
Installed 9 packages in 101ms
Hello from a package!
IP: 77.16.64.133
So when user only have uv installed,then code over will get Python 3.13 and install packages automatic.
Reply
#3
thank you snippsat for the reply,

1. this pyinstaller. my code contain A.I/Machine Learning thing(CPU only), can pyinstaller handle that?
2. uv thing, will this "uv python" conflict with "normal python"?
Reply
#4
I'm sure snippsat's advice is excellent. I just don't understand it!

Not sure if you are using windows or Linux. I don't know anything about windows. Apparently, you can change your path variable in windows like this:

Quote:Changing the Path Environment Variable
Because the Path environment variable is a list of many paths, you need to be careful when modifying it, so that you don’t overwrite the whole Path with just one path.

Typically, if you’re going to be doing that from PowerShell as a temporary environment variable, then you’ll need to append the new value with string concatenation:

$env:Path = "$env:Path;C:\new\path"

If you can do the above, the rest should work just like it does in Linux:

Send the module / package you have made to your friend. It is just a text file.
You don't even need to make mypackage.py executable.

Tell your friend to create a directory on his / her computer similar to this: /home/username/myPython/my_modules/ (just an example path)
You can use any name you like. Put the file mypackage.py in the directory /home/username/myPython/my_modules/

Now you have to tell the operating system about this path to files.
Then, when you write: import mypackage the operating system will look in /home/username/myPython/my_modules/ will find mypackage.py and make it available to your friend's programme.

You can temporarily append any path to your operating system $PATH variable from within a Python programme like this:

import sys
# tell the operating system about the new path to files
# temporarily append this path to $PATH
sys.path.append('/home/username/myPython/myModules/')
# now you can import mypackage, or any other home-made module, easily.
import mypackage

# below follows code for any programme you wish to write
In Linux you can also permanently add a path to your system by modifying the file .bashrc
This file is normally in your home directory.

Put this line at the end of the .bashrc file on a new line, using a simple text editor.

Quote:export PATH=/some/new/path:$PATH

Then in a bash terminal, apply the change like this on the command line:

Quote:source ~/.bashrc

Now, the path: /some/new/path is always be available to your search paths variable $PATH, so you don't need to do this:

import sys
# tell the operating system about the new path to files
# temporarily append this path to $PATH
sys.path.append('/home/username/myPython/myModules/')
Reply
#5
(Oct-12-2025, 04:30 AM)kucingkembar Wrote: 1. this pyinstaller. my code contain A.I/Machine Learning thing(CPU only), can pyinstaller handle that?
Pyinstaller can handle most MLlib(NumPy/SciPy/scikit-learn/PyTorch-CPU/TensorFlow-CPU),
but you have to mind data files, DLLs, and hidden imports.
So first build all in virtual environment,and use --onedir
--onefile is convenient but slower to start and trickier for huge libs. Begin with:
pyinstaller app.py  --onedir --collect-all sklearn --collect-all torch --collect-all torchvision
(Oct-12-2025, 04:30 AM)kucingkembar Wrote: 2. uv thing, will this "uv python" conflict with "normal python"?
No it will use OS python,or if no Python or wrong version on OS it will install version required automatic.
So example to using uv eg app.py to a user with no Python installed.
1. Install uv (once), if they don’t already have it:
powershell -ExecutionPolicy Bypass -c "irm https://astral.sh/uv/install.ps1 | iex"
2.
app.py
# /// script
# requires-python = ">=3.10"
# dependencies = [
#   "torch",
#   "torchvision",
# ]
# ///

import torch, torchvision

def main():
    print("Torch:", torch.__version__)
    print("Torchvision:", torchvision.__version__)
    x = torch.rand(2, 3)
    print("Sample tensor:", x)

if __name__ == "__main__":
    main()
3. run with uv run app.py
Output:
λ uv run app.py Installed 13 packages in 8.15s Torch: 2.8.0+cpu Torchvision: 0.23.0+cpu Sample tensor: tensor([[0.0983, 0.1069, 0.3095], [0.5128, 0.8607, 0.6505]])
So the uv way may be more easier with ML stuff,as it install all required on user PC.
Reply
#6
@Pedroski55
Q. "Not sure if you are using windows or Linux"
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
Reply
#7
@snippsat, the uv thing, how you install the package?
does this :
# /// script
# requires-python = ">=3.10"
# dependencies = [
#   "torch",
#   "torchvision",
# ]
# ///
alone enough?
Reply
#8
Yes when you just have uv,then the app.py will check if OS have Python if not uv will install it.
Then install package all this is automatic.

If i now test app.py on a new Pc.
# /// script
# requires-python = ">=3.10"
# dependencies = [
#   "torch",
#   "torchvision",
# ]
# ///
 
import torch, torchvision
 
def main():
    print("Torch:", torch.__version__)
    print("Torchvision:", torchvision.__version__)
    x = torch.rand(2, 3)
    print("Sample tensor:", x)
 
if __name__ == "__main__":
    main()
See under that uv fast it install PyTorch and all dependencies(13 packages) in 5-sec.
Then run code an PyTorch works.
From command line i use cmder,but command is the same in cmd/Powershell.
# Check that uv is installed
E:\div_code\ml
λ uv --version
uv 0.9.2 (141369ce7 2025-10-10)

# One file in folder
E:\div_code\ml
λ ls
app.py

# Run code it install 13 packages,it find python version if not it would have install Python automatic 
E:\div_code\ml
λ uv run app.py
Installed 13 packages in 4.78s
Torch: 2.8.0+cpu
Torchvision: 0.23.0+cpu
Sample tensor: tensor([[0.7076, 0.1823, 0.9419],
        [0.2624, 0.1301, 0.3833]])
Reply
#9
thank you snippsat for the answer, i add you a reputation point
Reply
#10
sorry snippsat for additinal question.
while True:
  try:
    import thepackage
  except:
    subproces("pip install thepackage")#only example
  else:
    break
(Oct-11-2025, 08:51 PM)snippsat Wrote: The code you have will not work.
why this code willl not work?
i tried it sometime and work without problem.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Best way to secure API key when sharing quarinteen 2 1,847 Jan-19-2024, 04:46 PM
Last Post: deanhystad
  How to install modules for 2.7 (or sharing from 3.8)) Persisto 2 4,927 Dec-31-2021, 02:33 PM
Last Post: Persisto
  multiprocessing and sharing object cyrduf 0 3,394 Feb-02-2021, 08:16 PM
Last Post: cyrduf
  sharing variables between two processes Kiyoshi767 1 3,181 Nov-07-2020, 04:00 AM
Last Post: ndc85430
  Run a ready to go Python project flaviu2 1 2,562 Nov-03-2020, 08:55 PM
Last Post: snippsat
  Sharing my code emirasal 2 3,367 Oct-04-2020, 02:21 PM
Last Post: emirasal
  Sharing X Axis in Sub plots JoeDainton123 1 3,944 Aug-22-2020, 04:11 AM
Last Post: deanhystad
  Instances sharing attributes midarq 4 4,854 Sep-20-2019, 11:13 AM
Last Post: midarq
  Avoid hard-coded folderPath in the code (let users provide it) SunOffice024 6 9,006 Sep-08-2018, 06:41 PM
Last Post: SunOffice024
  Sharing variables across modules j.crater 4 5,877 Jul-30-2018, 09:09 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020