Using pyinstaller to package a py file, it's default behavior is to put all the dependencies in the root folder with the exe file. This is not neat to end users, looks so unneat.
A simple hello.py could end up a mess in the exe folder:

not to mention big projects that use many site-packages modules would end up like.
So I wish there could be a mode, or a flag, to put all the dependency files into one subfolder of the exe folder root.
It is viable, now I can make this by modifying the spec file and adding a hook file:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(
['hello.py'],
pathex=[],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=['hook.py'],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
# ============================Modifying Part==============================================
import re
import os
from os import sep
a.binaries.extend([(x[1][x[1].find(x[0].replace('.', sep)):],
x[1],
'BINARY')
for x in a.pure])
a.pure.clear()
def new_dest(package: str):
if package == 'base_library.zip' or re.match(r'python\d+.dll', package):
return package
return 'libs' + os.sep + package
a.binaries = [(new_dest(x[0]), x[1], x[2]) for x in a.binaries]
# =============================================================================
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(
pyz,
a.scripts,
[],
exclude_binaries=True,
name='hello',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
coll = COLLECT(
exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='hello',
)
This is the hook.py:
import sys
from pathlib import Path
BASE_DIR = Path(__file__).parent
for p in sys.path.copy():
relative_p = Path(p).relative_to(BASE_DIR)
new_p = BASE_DIR / 'libs' / relative_p
sys.path.insert(0, str(new_p))
And the packaged folder looks like this, very nice and clean:

Of course, the exe file runs normally, since I have add the libs folder to the sys.path in the hook.py.
I wish this kind of feature could be add to PyInstaller, so users can benifit from this by simply adding an option when using pyinstaller.
The Zen of Python:
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Using pyinstaller to package a py file, it's default behavior is to put all the dependencies in the root folder with the exe file. This is not neat to end users, looks so unneat.
A simple hello.py could end up a mess in the exe folder:
not to mention big projects that use many site-packages modules would end up like.
So I wish there could be a mode, or a flag, to put all the dependency files into one subfolder of the exe folder root.
It is viable, now I can make this by modifying the
specfile and adding a hook file:This is the
hook.py:And the packaged folder looks like this, very nice and clean:
Of course, the exe file runs normally, since I have add the
libsfolder to thesys.pathin thehook.py.I wish this kind of feature could be add to PyInstaller, so users can benifit from this by simply adding an option when using pyinstaller.
The Zen of Python: