0


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

5
  • If you first cd myapp then call python main.py does it work? Commented Sep 3, 2024 at 19:36
  • @sytech, At first I had installed pywin32 with pip on my pc and yes it worked, but for testing this standalone packge I uninstalled pywin32 from my pc then I tried again and that raised this error : ModuleNotFoundError: No module named 'pywintypes' Commented Sep 3, 2024 at 19:44
  • @sytech, yes I get the error Commented Sep 3, 2024 at 20:11
  • Tried it myself--the issue is that for some reason, the python importer can't find the pywin32 package when it's installed in the local directory. (If you install a different package to /appdir and import it at the top of your main.py and run it, it works as expected). Unfortunately I can't tell at a glance what the issue is--I know that pywin32 is indeed a strange package. I think it would take searching through the documentation of the python path based finder to understand why the package is not being found. Commented Sep 3, 2024 at 20:16
  • If you want to rephrase your question to make it more accurate, I would title it "python importer can't find the pywin32 package when it's installed to local directory". The MRE (minimum reproducible example) is: 1) Create a folder /myapp 2) Execute pip install -t myapp pywin32 3) Create /myapp/main.py with a single line import pywin32 4) Run main.py and get the error Commented Sep 3, 2024 at 20:22

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.