[Python] Why does a python script find the win32api - but not when run as a subprocess from...

Discussão em 'Python' iniciado por Stack, Março 24, 2025.

  1. Stack

    Stack Membro Participativo

    I've got a script which when called via

    (.venv) PS D:\Users\user4\MyPython\AllDiscs> py -m list_discs_with_folders dlist.txt


    works as desired, but fails when called as a subprocess from another one:

    import os
    import time
    from os.path import getmtime
    import datetime
    import sys

    import win32api
    from difflib import SequenceMatcher
    import shutil


    def get_drive_label(drive_path):
    try:
    volume_info = win32api.GetVolumeInformation(drive_path)
    return volume_info[0] # name of the volume
    except Exception as e:
    print(f"Error: {e}")
    return None
    ...


    This codes works as expected (getting the volume information via win32api). But if i run the script from a second one:

    import tkinter as tk
    from tkinter import ttk, scrolledtext
    import subprocess

    root = tk.Tk()
    root.title("Test WinAPI")

    def build_sel():
    subprocess.run(["python", "list_discs_with_folders.py", "dlist.txt"])

    btn_run = ttk.Button(root, text="Build selected", command=build_sel)
    btn_run.pack()

    root.mainloop()


    This gives an error:

    Traceback (most recent call last):
    File "D:\Users\user4\MyPython\AllDiscs\list_discs_with_folders.py", line 7, in <module>
    import win32api
    ModuleNotFoundError: No module named 'win32api'


    Why does the interpreter find win32api in one case but not in the other?

    Continue reading...

Compartilhe esta Página