Python Forum

Full Version: Install a module to a specific to Python Installation (one of many))
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Greetings!
It is kind of confusion to explain. Sorry about that!
I have 4 different Python versions installed on a Windows machine.
Python version 3.11 is a main one, it is setup in the Envs of the PC and the other ones installed without the Envs and used by the user with the lines in the scripts:
#!"C:\Python308\python.exe"
#!"C:\Python306\python.exe"
#!"C:\Python027\python.exe"

I'd like to install a new module for the "C:\Python308\python.exe", I understand PIP would not work for the local installation. How could I install/upgrade a module?
Thank you.
Tester_V
Usually with newer Python version, the tool py.exe is installed on Windows. This tool helps to address the wanted Python interpreter.

https://docs.python.org/3/using/windows....or-windows

To list all found installations:
PS C:\Users\XXX> py -0
 -V:3.13 *        Python 3.13 (64-bit)
 -V:3.12          Python 3.12 (64-bit)
You could use a batch file, PowerShell, Python or other available languages which are able to start programs.
I made a short example with Python, which does not use py.exe.
It searches only for user instlallations of Python, not system wide installation and not from MS-Store.

import os
import subprocess
from pathlib import Path

# by default the Python installer prefers a user installation which
# doesn't require admin rights
# converting the str to a Path object
# localappdata is the place where your user data is, which is not sychronized with OneDrive.

local_app_dir = Path(os.environ.get("localappdata"))
package = input("Packages you want to install (split by whitespace): ").split()

# using glob to find the python.exe files in the subdirectories
for python_exe in local_app_dir.glob("Programs/Python/Python*/python.exe"):
    # run the interpreter once to show the version
    proc = subprocess.run([python_exe, "--version"], capture_output=True, encoding="utf8")
    print(f"Installing `{' '.join(package)}` for {proc.stdout.strip()}", end=" ", flush=True)

    # installing a package on this python version
    # pip is loaded as module
    # output is suppressed, but if an error happens, it will print [ FAIL ]
    # using argument unpacking: unpack packages into the list
    proc = subprocess.run([python_exe, "-m", "pip", "install", *package], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
    if proc.returncode == 0:
        print("[  OK  ]")
    else:
        print("[ FAIL ]")
There are much better methods.
Usually tools are used to automate installation of dependencies and tests.

Testing with different Python-Versions: https://pypi.org/project/tox/
Replacement for pip: https://github.com/astral-sh/uv

uv is really fast with installing dependencies, but it works a bit different. Still have to learn how this tool works.
(Oct-28-2024, 10:05 PM)tester_V Wrote: [ -> ]I'd like to install a new module for the "C:\Python308\python.exe", I understand PIP would not work for the local installation. How could I install/upgrade a module?
As mention use py.
Here i install Requests to Python 3.8.
G:\div_code
位 py -3.8 -m pip install requests
Collecting requests
.....  
Successfully installed certifi-2024.8.30 charset-normalizer-3.4.0 idna-3.10 requests-2.32.3 urllib3-2.2.3
As mention uv is good.
Here it download and install a full python 3.13 version,and i activate environment.
G:\div_code\py_313
位 uv venv --python 3.13.0
Using CPython 3.13.0
Creating virtual environment at: .venv
Activate with: .venv\Scripts\activate

G:\div_code\py_313
位 .venv\Scripts\activate

G:\div_code\py_313
(py_313) 位 python --version
Python 3.13.0
Eg install Request to it,it's fast馃殌
G:\div_code\py_313
(py_313) 位 uv pip install requests
Resolved 5 packages in 440ms
Prepared 5 packages in 394ms
鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒鈻戔枒 [0/5] Installing wheels...
warning: Failed to hardlink files; falling back to full copy. This may lead to degraded performance.
         If the cache and target directories are on different filesystems, hardlinking may not be supported.
         If this is intentional, set `export UV_LINK_MODE=copy` or use `--link-mode=copy` to suppress this warning.
Installed 5 packages in 449ms
 + certifi==2024.8.30
 + charset-normalizer==3.4.0
 + idna==3.10
 + requests==2.32.3
 + urllib3==2.2.3

G:\div_code\py_313
(py_313) 位 python
Python 3.13.0 (main, Oct 16 2024, 00:33:24) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> exit
 
G:\div_code\py_313
(py_313) 位