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

静态(static)编译 Python 3.10.14 报错无法编译成功,有朋友可以帮忙看看吗?

  •  
  •   coolair · 107 天前 · 1302 次点击
    这是一个创建于 107 天前的主题,其中的信息可能已经有所发展或是发生改变。

    操作系统:CentOS Linux release 8.5.2111 Python:3.10.14

    参考 https://wiki.python.org/moin/BuildStatically 进行编译:

    ./configure LDFLAGS="-static" CFLAGS="-static" CPPFLAGS="-static" --disable-shared --enable-optimizations
    make LDFLAGS="-static" LINKFORSHARED=" "
    make altinstall
    

    最后一步 make altinstall 失败:

    copying build/scripts-3.10/pydoc3.10 -> /usr/local/bin
    copying build/scripts-3.10/idle3.10 -> /usr/local/bin
    copying build/scripts-3.10/2to3-3.10 -> /usr/local/bin
    changing mode of /usr/local/bin/pydoc3.10 to 755
    changing mode of /usr/local/bin/idle3.10 to 755
    changing mode of /usr/local/bin/2to3-3.10 to 755
    rm /usr/local/lib/python3.10/lib-dynload/_sysconfigdata__linux_x86_64-linux-gnu.py
    rm -r /usr/local/lib/python3.10/lib-dynload/__pycache__
    /usr/bin/install -c -m 644 ./Misc/python.man \
    	/usr/local/share/man/man1/python3.10.1
    if test "xupgrade" != "xno"  ; then \
    	case upgrade in \
    		upgrade) ensurepip="--altinstall --upgrade" ;; \
    		install|*) ensurepip="--altinstall" ;; \
    	esac; \
    	 ./python -E -m ensurepip \
    		$ensurepip --root=/ ; \
    fi
    Traceback (most recent call last):
      File "/root/Python-3.10.14/Lib/subprocess.py", line 69, in <module>
        import msvcrt
    ModuleNotFoundError: No module named 'msvcrt'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/root/Python-3.10.14/Lib/runpy.py", line 187, in _run_module_as_main
        mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
      File "/root/Python-3.10.14/Lib/runpy.py", line 146, in _get_module_details
        return _get_module_details(pkg_main_name, error)
      File "/root/Python-3.10.14/Lib/runpy.py", line 110, in _get_module_details
        __import__(pkg_name)
      File "/root/Python-3.10.14/Lib/ensurepip/__init__.py", line 4, in <module>
        import subprocess
      File "/root/Python-3.10.14/Lib/subprocess.py", line 74, in <module>
        import _posixsubprocess
    ModuleNotFoundError: No module named '_posixsubprocess'
    make: *** [Makefile:1290: altinstall] Error 1
    

    实际上以及编译安装成功,只是 pip3 无法使用:

    # pip3
    Traceback (most recent call last):
      File "/usr/local/lib/python3.10/subprocess.py", line 69, in <module>
        import msvcrt
    ModuleNotFoundError: No module named 'msvcrt'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/usr/bin/pip3", line 5, in <module>
        from pip._internal.cli.main import main
      File "/usr/local/lib/python3.10/site-packages/pip/_internal/cli/main.py", line 9, in <module>
        from pip._internal.cli.autocompletion import autocomplete
      File "/usr/local/lib/python3.10/site-packages/pip/_internal/cli/autocompletion.py", line 10, in <module>
        from pip._internal.cli.main_parser import create_main_parser
      File "/usr/local/lib/python3.10/site-packages/pip/_internal/cli/main_parser.py", line 5, in <module>
        import subprocess
      File "/usr/local/lib/python3.10/subprocess.py", line 74, in <module>
        import _posixsubprocess
    ImportError: /usr/local/lib/python3.10/lib-dynload/_posixsubprocess.cpython-310-x86_64-linux-gnu.so: undefined symbol: PyTuple_Type
    
    # python3
    Python 3.10.14 (main, Aug 15 2024, 10:07:53) [GCC 8.5.0 20210514 (Red Hat 8.5.0-4)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import subprocess
    Traceback (most recent call last):
      File "/usr/local/lib/python3.10/subprocess.py", line 69, in <module>
        import msvcrt
    ModuleNotFoundError: No module named 'msvcrt'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python3.10/subprocess.py", line 74, in <module>
        import _posixsubprocess
    ImportError: /usr/local/lib/python3.10/lib-dynload/_posixsubprocess.cpython-310-x86_64-linux-gnu.so: undefined symbol: PyTuple_Type
    
    13 条回复    2024-08-27 19:05:42 +08:00
    ysc3839
        1
    ysc3839  
       107 天前 via Android
    为什么要静态编译?原始需求是什么?
    lt0136
        2
    lt0136  
       107 天前 via Android
    coolair
        4
    coolair  
    OP
       107 天前
    @GeekGao #3 虽然报错一样,但是情况不一样。静态编译以后,动态链接库全是失败的。
    GeekGao
        5
    GeekGao  
       107 天前
    @coolair 为啥静态编译? 源码编译不就行了么
    proxytoworld
        6
    proxytoworld  
       107 天前
    ldd 显示是 static link 吗
    coolair
        7
    coolair  
    OP
       107 天前
    @proxytoworld #6

    # ldd /usr/local/bin/python3
    not a dynamic executable
    proxytoworld
        8
    proxytoworld  
       107 天前
    我注意到你的报错里面有:
    import msvcrt
    ModuleNotFoundError: No module named 'msvcrt'

    msvcrt 是 Windows 上的 runtime ,你可能编译错了
    coolair
        9
    coolair  
    OP
       107 天前
    @proxytoworld #8 没有编译错,我编译好的 Python 可以运行,只是原本依赖动态链接库,导致 pip3 和一些模块不能正常使用。
    houzhiqiang
        10
    houzhiqiang  
       107 天前
    编译完后要跑自带的测试集看编译是否正确的,越老的 python 版本越难静态编译
    不然去 github 上找一个别人配置好的静态编译工程
    我也静态编译过 Python3.10 但是不能完整的过测试集

    $ ldd build/bin/python3.10
    not a dynamic executable
    $ file build/bin/python3.10
    build/bin/python3.10: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, BuildID[sha1]=546463ab44b360fbe42e38f843548bbfc6ce4d61, for GNU/Linux 3.2.0, with debug_info, not stripped
    $ ./build/bin/python3.10 -m pip install --upgrade pip
    Looking in indexes: https://mirrors.cloud.aliyuncs.com/pypi/simple/
    Requirement already satisfied: pip in ./build/lib/python3.10/site-packages (21.2.4)
    Collecting pip
    Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/d4/55/90db48d85f7689ec6f81c0db0622d704306c5284850383c090e6c7195a5c/pip-24.2-py3-none-any.whl (1.8 MB)
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 21.1 MB/s eta 0:00:00
    Installing collected packages: pip
    Attempting uninstall: pip
    Found existing installation: pip 21.2.4
    Uninstalling pip-21.2.4:
    Successfully uninstalled pip-21.2.4
    Successfully installed pip-24.2
    guanzhangzhang
        11
    guanzhangzhang  
       106 天前
    musl 的 rootfs 里编译试试?
    UN2758
        12
    UN2758  
       96 天前
    看样子是 ms 的 rt 库报错?为什么你编译 linux 的 python ,会去调 ms 的库?是不是 target binary os 之类配置错了
    deplives
        13
    deplives  
       95 天前
    Traceback (most recent call last):
    File "/root/Python-3.10.14/Lib/subprocess.py", line 69, in <module>
    import msvcrt
    ModuleNotFoundError: No module named 'msvcrt'

    这个苦是 Windows 上才有的,肯定是编译错目标了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2685 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:06 · PVG 20:06 · LAX 04:06 · JFK 07:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.