Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please add a clean folder mode #7687

Closed
HaujetZhao opened this issue Jun 6, 2023 · 1 comment · Fixed by #7713
Closed

Please add a clean folder mode #7687

HaujetZhao opened this issue Jun 6, 2023 · 1 comment · Fixed by #7713
Labels
feature Feature request solution:duplicate Resolved: This issue is a duplicate.

Comments

@HaujetZhao
Copy link

HaujetZhao commented Jun 6, 2023

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:

image

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:

image

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!
@HaujetZhao HaujetZhao added feature Feature request triage Please triage and relabel this issue labels Jun 6, 2023
@bwoodsend
Copy link
Member

Duplicate of #5575. For your current proof of concept, any package with its data files banished to libs will still look for those data files in their original locations so the show stops there. A lot of paths need to be rewritten for this to really work.

@bwoodsend bwoodsend closed this as not planned Won't fix, can't repro, duplicate, stale Jun 6, 2023
@bwoodsend bwoodsend added solution:duplicate Resolved: This issue is a duplicate. and removed triage Please triage and relabel this issue labels Jun 6, 2023
@bwoodsend bwoodsend linked a pull request Jul 10, 2023 that will close this issue
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature Feature request solution:duplicate Resolved: This issue is a duplicate.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants