V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
garywill
V2EX  ›  Python

压缩 Python 代码,但不要改变换行方式,有这样的工具吗?

  •  
  •   garywill ·
    garywill · 4 天前 · 2503 次点击

    我需要一个能够最小化 python 代码文件,但不改变我的换行方式的代码压缩器。(为了便于报错时查错,所以行号不想改变)

    具体点的说话,希望工具能够:

    1. 删除所有注释,以及末尾空格
    2. 替换所有开头的空格缩进为 tab
    3. 最小化所有函数内的变量名,但不要改变任何全局对象名
    21 条回复    2024-09-25 16:54:31 +08:00
    mightybruce
        1
    mightybruce  
       4 天前   ❤️ 2
    https://python-minifier.com/
    看看这个是否满足
    q8515620
        2
    q8515620  
       4 天前
    不改变行号这个可能现有的工具都不支持吧?
    br_wang
        3
    br_wang  
       4 天前
    python 生态有类似 sourcemap 的工具么?
    SilenceRet
        4
    SilenceRet  
       4 天前
    py 这种逆天的以缩进控制结构的东西 还能压缩吗 =_=!
    zepc007
        5
    zepc007  
       4 天前   ❤️ 1
    python 代码又不是 2C 的,此举意义何在?
    drymonfidelia
        6
    drymonfidelia  
       4 天前
    不改变行号的话 winrar 可以做到 就是压缩完的不能被 cpython 直接执行
    @q8515620
    treblex
        7
    treblex  
       4 天前
    二进制可以用 upx ,可以压掉 3/4
    tomczhen
        8
    tomczhen  
       4 天前
    zipimport
    crackidz
        9
    crackidz  
       4 天前
    你这样,要不直接提供 pyc 吧
    louis16s
        10
    louis16s  
       4 天前
    有这个必要吗
    sazima
        11
    sazima  
       4 天前
    把项目代码压缩成 zip , 写一个启动器解压代码并执行。
    sazima
        12
    sazima  
       4 天前
    研究一下, 不把文件实际解压到磁盘上,只把代码读到内存中也是可以的
    ClericPy
        13
    ClericPy  
       4 天前
    https://github.com/ClericPy/morebuiltins/blob/master/morebuiltins/utils.py#L411

    以前做过类似的 code_inline ,保留行号应该也留个差不多,毕竟只是压缩了一下。

    报错查错自己写个 format error 也可以找到是哪行哪句报哪个错

    https://github.com/ClericPy/morebuiltins/blob/master/morebuiltins/utils.py#L782
    guiyumin
        14
    guiyumin  
       3 天前
    既然都用到编译器了
    那不如编译的时候把缩紧改为大括号,然后把这个编译后的代码运行在一个支持大括号的 python 解释器上,完美

    至于这个支持大括号的 python 解释器,我觉得可以考虑给 cpython 写个插件之类的
    Reficul
        15
    Reficul  
       3 天前
    Python 支持从 zip 压缩包里执行代码,可以看下:
    1. https://peps.python.org/pep-0273/
    2. https://peps.python.org/pep-0441/
    Reficul
        16
    Reficul  
       3 天前
    另外提一嘴,一般文本文件压缩率在 10x 左右,不管是 gzip 还是 zip 都差不多。
    mayli
        17
    mayli  
       3 天前
    zipimport + pyc?
    lekai63
        18
    lekai63  
       3 天前 via iPhone
    不如 pyc 交付?
    qiuhang
        19
    qiuhang  
       3 天前
    编译成二进制交付?比如用 nuitka
    ylei
        20
    ylei  
       3 天前
    代码 -》 AST -》 代码
    enrolls
        21
    enrolls  
       3 天前
    有意义吗?有作用吗?哪怕你只给 pyc ,能运行就能 hook ,能 hook 就能 export 。打包 pyd 吧,OD 一样能看,只是繁琐而已。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2445 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 15:21 · PVG 23:21 · LAX 08:21 · JFK 11:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.