How to Convert a Python Script to an Executable File (.exe) include PDFTron

Product: Python PDF library (PDFNetPython3)

Product Version: 9.1.0

I want to share my Solution about “How to Convert a Python Script to an Executable File (.exe) include PDFTron”

Firstly, I used PyInstaller to package a python script to exe. For example: “pyinstaller -F main.py”

Unfortunately, I got an exception:

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    from PDFNetPython3.PDFNetPython import *
  File "<frozen importlib._bootstrap>", line 991, in _find_and_loa
  File "<frozen importlib._bootstrap>", line 975, in _find_and_loa
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocke
  File "PyInstaller\loader\pyimod03_importers.py", line 476, in ex
  File "PDFNetPython3\__init__.py", line 2, in <module>
AttributeError: module 'site' has no attribute 'addsitedir'
[4000] Failed to execute script 'main' due to unhandled exception

Then I modified the code of “PDFNetPython3_init_.py” in “…\Python38\Lib\site-packages” like this:

import site, os, subprocess, platform
site.addsitedir(os.path.dirname(__file__))
......
from PDFNetPython import *

TO

import os, subprocess, platform
# site.addsitedir(os.path.dirname(__file__))
......
from .PDFNetPython import *

The exception disappeared, and I got a main.exe to execute standalone.

1 Like