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
Septembers
V2EX  ›  Python

如何静态编译构建 Python

  •  1
     
  •   Septembers · 2015-03-29 06:58:08 +08:00 · 7908 次点击
    这是一个创建于 3287 天前的主题,其中的信息可能已经有所发展或是发生改变。
    按照 https://wiki.python.org/moin/BuildStatically 构建出来依然有动态库依赖
    请问是我的打开方式错误吗?


    Ubuntu Desktop 14.04.2 ( x86_64 )
    12 条回复    2018-04-26 14:42:43 +08:00
    memorybox
        1
    memorybox  
       2015-03-29 08:28:57 +08:00   ❤️ 1
    Septembers
        2
    Septembers  
    OP
       2015-03-29 08:31:04 +08:00
    @memorybox 感谢 但是我还是需要知道如何静态编译官方版本 谢谢
    AstroProfundis
        3
    AstroProfundis  
       2015-03-29 11:33:23 +08:00   ❤️ 1
    ./configure --xxx --xxx LDFLAGS="-static"
    make xxx LDFLAGS="-all-static"

    这样试试
    Septembers
        4
    Septembers  
    OP
       2015-03-29 11:38:40 +08:00
    @AstroProfundis
    gcc: error: unrecognized command line option '-all-static'
    v2exhehehehe
        5
    v2exhehehehe  
       2015-03-29 13:49:08 +08:00   ❤️ 1
    1楼给出的项目倒是有说明怎么扩展、自定义、重编译的说明,可以去看看里面的build.sh

    你给出的页面有这样一句话:This page is a placeholder until MicahElliott (or anyone) figures out all the steps to make this work.
    这只是占位符,直到有人弄清所有步骤,看样子还没有人静态编译成功过,祝lz好运。
    Septembers
        6
    Septembers  
    OP
       2015-03-29 14:02:58 +08:00
    @wongsyrone
    我需要静态构建Python 2.7.3(那个项目有版本强依赖 唉
    memorybox
        7
    memorybox  
       2015-03-29 14:16:09 +08:00   ❤️ 1
    我曾经在centos上做过Python2.7.8的编译, 也是碰到了各种问题,后来直接用人家现成的了。

    之前在cpyUG邮件列表里面见过有人做类似的东西:

    https://github.com/auxten/miniPyRuntime

    这个是基于python2.7.3的,但是它依赖于这些库:
    $:ldd dist/bin/python
    linux-vdso.so.1 => (0x00007fff170d3000)
    libkrb5.so.3 => /lib64/libkrb5.so.3 (0x0000003a8aa00000)
    libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x0000003a8ba00000)
    libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x0000003a8a600000)
    libz.so.1 => /lib64/libz.so.1 (0x0000003a89a00000)
    libsqlite3.so.0 => /usr/lib64/libsqlite3.so.0 (0x0000003f75600000)
    libbz2.so.1 => /lib64/libbz2.so.1 (0x0000003f79a00000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003f75200000)
    libdl.so.2 => /lib64/libdl.so.2 (0x0000003f74e00000)
    libutil.so.1 => /lib64/libutil.so.1 (0x0000003f78600000)
    libm.so.6 => /lib64/libm.so.6 (0x00007f1a1bb78000)
    libc.so.6 => /lib64/libc.so.6 (0x0000003f74a00000)
    libcom_err.so.2 => /lib64/libcom_err.so.2 (0x0000003a8a200000)
    libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x0000003a8b600000)
    libresolv.so.2 => /lib64/libresolv.so.2 (0x0000003f76e00000)
    libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003a89e00000)
    /lib64/ld-linux-x86-64.so.2 (0x0000003f74600000)

    可以参考一下它的裁剪方法。

    PS:这个项目我没有用过。我用了之前推荐的StaticPython很长一段时间,非常稳定。
    Septembers
        8
    Septembers  
    OP
       2015-03-29 16:04:38 +08:00
    @memorybox @wongsyrone @AstroProfundis
    现在已经实现静态编译但是独立复制出来无法使用
    编译环境信息:
    Kernel 3.16
    Ubuntu Desktop 14.04.2 LTS
    GCC 4.8.2
    编译:
    curl https://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz | tar zxv
    cd Python-2.7.3
    ./configure --disable-shared LDFLAGS="-s -static -static-libgcc" CPPFLAGS="-static"
    echo '*static*' > Modules/Setup.local
    make python -j LINKFORSHARED=""
    测试:
    ➜ Python-2.7.3 ldd ./python
    not a dynamic executable
    ➜ Python-2.7.3 ./python
    Python 2.7.3 (default, Mar 29 2015, 15:51:50)
    [GCC 4.8.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> exit()
    ➜ Python-2.7.3 cp ./python ..
    ➜ Python-2.7.3 ../python
    Could not find platform independent libraries <prefix>
    Could not find platform dependent libraries <exec_prefix>
    Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
    ImportError: No module named site
    ➜ Python-2.7.3

    请问这个问题怎么解决谢谢
    AstroProfundis
        9
    AstroProfundis  
       2015-03-29 19:58:03 +08:00   ❤️ 1
    @Septembers 这个应该是文件静态编译好了,但你复制走的时候要带上全部的 lib, python 自己的那些 lib 还是和主程序分开的(各种 .py 之类的),解掉的依赖应该是对系统 glibc 的,印象中似乎见过全部编译成一个可执行文件的 python, 好像就是前面那个 staticpython
    hahafofo
        10
    hahafofo  
       2015-05-05 16:51:46 +08:00   ❤️ 1
    解决ImportError: No module named site,如果是c程序调用,链接到libpython2.7.a,在c程序里面加上这3行就可以了。

    Py_NoSiteFlag = 1;
    Py_FrozenFlag = 1;
    Py_IgnoreEnvironmentFlag = 1;


    Py_SetPythonHome("");
    Py_SetProgramName("");

    如果不是编译c程序,需要设置$PYTHONHOME
    jamesarch
        11
    jamesarch  
       2018-04-26 14:41:03 +08:00
    jamesarch
        12
    jamesarch  
       2018-04-26 14:42:43 +08:00
    。我试了下官方构建可以的 _(:з)∠)_
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   970 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 21:54 · PVG 05:54 · LAX 14:54 · JFK 17:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.