pythonpywin32python-standalone

Creating a standalone python executable with pywin32 and missed required module



I'm going to create a standalone python executable that rely on a python interpreter on windows (I don't want to create an exe file).
So, I'm used this helpful article:
https://n8henrie.com/2022/08/easily-create-almost-standalone-python-executables-with-the-builtin-zipapp-module/#google_vignette
My script python uses pywin32 for converting word to pdf,
After running this command:
> python -m pip install -t myapp pywin32
I tried to test working of the standalone script on myapp directory. so I ran:
> python .\myapp\main.py
And I get this error:
Traceback (most recent call last): File "D:\Parsa Projects\Python-PDF\myapp\main.py", line 3, in <module> import win32com.client File "D:\Parsa Projects\Python-PDF\myapp\win32com\__init__.py", line 8, in <module> import pythoncom File "D:\Parsa Projects\Python-PDF\myapp\pythoncom.py", line 2, in <module> import pywintypes ModuleNotFoundError: No module named 'pywintypes'


I searched about the problem and I found installing pypiwin32 solves the problem, so I Installed it on the myapp directory, but it not solved the problem.
Can anyone help me?
What I can do?
This is my python code:

import argparse
import pathlib
import win32com.client
import os

def convert_word_to_pdf(word_file_path, pdf_file_path):
    if not os.path.exists(word_file_path):
        raise FileNotFoundError(f"The file {word_file_path} does not exist.")
    word = win32com.client.Dispatch('Word.Application')
    word.Visible = False

    try:
        doc = word.Documents.Open(word_file_path)
        doc.SaveAs(pdf_file_path, FileFormat=17)
        doc.Close()
    except Exception as e:
        print(f"An error occurred: {e}")
    finally:
        word.Quit()

def main():
    filepath = os.path.abspath('D:\\Parsa Projects\\Python-PDF\\file-sample_100kB.docx')
    print(filepath)
    # pdf_path = pathlib.Path('./file-sample_100kB.pdf').absolute()
    pdf_path = os.path.abspath('D:\\Parsa Projects\\Python-PDF\\file-sample_100kB.pdf')
    convert_word_to_pdf(filepath, pdf_path)
    # parser = argparse.ArgumentParser("Arguments")

    # if os.environ.get("MY_ENV_VAR") is not None:
    #     parser.add_argument("--my-env-var", required=True, action="store_true")
    
    # if os.environ.get("MY_ENV_VAR2") is not None:
    #     parser.add_argument("--my-env-var2", required=True, action="store_true")
    
    # parser.parse_args()
if __name__ == "__main__":
    # main()
    # exit()
    # doc_path = pathlib.Path('./file-sample_100kB.docx').absolute()
    filepath = os.path.abspath('D:\\Parsa Projects\\Python-PDF\\file-sample_100kB.docx')
    print(filepath)
    # pdf_path = pathlib.Path('./file-sample_100kB.pdf').absolute()
    pdf_path = os.path.abspath('D:\\Parsa Projects\\Python-PDF\\file-sample_100kB.pdf')
    convert_word_to_pdf(filepath, pdf_path)


For making the standalone python package, On this my directory D:\Parsa Projects\Python-PDF I opened cmd and ran:


> mkdir myapp
> xcopy /f /y main.py .\myapp\main.py
> python -m pip install -t myapp pywin32 pypiwin32
> python -m zipapp -p "C:\Users\Parsa\AppData\Local\Programs\Python\Python312\python.exe" -m main:main -c -o myapps myapp


Solution

  • I found my issue was related to some metrics, like the library must supports some features and local paths must add to sys.path at the top of codes.
    So:
    1 - I replaced pywin32 package with comtypes package, to can support the program for multiplatform and not consist to the os versions.
    2 - Add the local paths and package address to sys.path:

    import os
    
    # SYS Import Local Packages Installed
    path = os.path.realpath(os.path.abspath(__file__))
    sys.path.insert(0, os.path.dirname(os.path.dirname(path)))
    sys.path.append(os.path.join(os.path.dirname(__file__),"."))
    sys.path.append(os.path.join(os.path.dirname(__file__),"packages"))
    # from packages import comtypes
    from packages.comtypes import client
    


    3 - Run this command with administrator access:

    
    > python -m zipapp -p "C:\Users\Parsa\AppData\Local\Programs\Python\Python312\python.exe" -m
    main:main -c -o myapps myapp