<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>setup &#8211; bhzhuOS爱好者(原StartOS爱好者)</title>
	<atom:link href="https://www.bhzhu203.com/tag/setup/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.bhzhu203.com</link>
	<description>QQ群号125732839</description>
	<lastBuildDate>Tue, 17 Jun 2025 09:21:18 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.7</generator>
	<item>
		<title>静态编译 Python3 程序指南</title>
		<link>https://www.bhzhu203.com/2025/06/17/%e9%9d%99%e6%80%81%e7%bc%96%e8%af%91-python3-%e7%a8%8b%e5%ba%8f%e6%8c%87%e5%8d%97/</link>
		
		<dc:creator><![CDATA[bhzhu203]]></dc:creator>
		<pubDate>Tue, 17 Jun 2025 09:21:18 +0000</pubDate>
				<category><![CDATA[linux知识]]></category>
		<category><![CDATA[modules]]></category>
		<category><![CDATA[setup]]></category>
		<category><![CDATA[编译]]></category>
		<guid isPermaLink="false">https://www.bhzhu203.com/?p=483</guid>

					<description><![CDATA[1. 模块选择与确认 首先确定要静态编译进Python的模块，例如需要将asyn [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h1 class="wp-block-heading"></h1>



<h2 class="wp-block-heading">1. 模块选择与确认</h2>



<p>首先确定要静态编译进Python的模块，例如需要将asyncio模块静态编译到python3。可以通过查看<code>Modules/Setup</code>文件来了解详细的官方库编译项。</p>



<h2 class="wp-block-heading">2. 清理源码目录</h2>



<p>执行以下命令将源码目录恢复到初始状态：</p>



<pre class="wp-block-code"><code>make distclean</code></pre>



<h2 class="wp-block-heading">3. 配置 <code>Modules/Setup.local</code></h2>



<p>将需要静态编译的模块信息写入<code>Modules/Setup.local</code>文件，例如：</p>



<pre class="wp-block-code"><code>*static*
_asyncio _asynciomodule.c
_socket socketmodule.c
math mathmodule.c _math.c -DPy_BUILD_CORE_MODULE
select selectmodule.c
array arraymodule.c
_posixsubprocess _posixsubprocess.c
_contextvars _contextvarsmodule.c
_struct _struct.c
binascii binascii.c</code></pre>



<h2 class="wp-block-heading">4. 执行 <code>configure</code></h2>



<p>运行以下命令进行配置，确保环境中没有缺少编译依赖：</p>



<pre class="wp-block-code"><code>./configure LDFLAGS="-static" --disable-shared</code></pre>



<p>如果<code>Modules/Setup.local</code>被修改或覆盖，请重新写入该文件并再次执行此步骤。若仍存在问题，可检查configure过程中的报错信息。</p>



<h2 class="wp-block-heading">5. 编译 Python3 程序</h2>



<p>根据实际情况进行多线程编译：</p>



<pre class="wp-block-code"><code>make -j10 LDFLAGS="-static" LINKFORSHARED=" "</code></pre>



<h2 class="wp-block-heading">6. 测试运行与静态链接验证</h2>



<p>在当前目录直接测试运行程序，确保没有缺失模块：</p>



<pre class="wp-block-code"><code>ldd ./python   # 查看执行文件是否为静态编译
./python xxxx.py</code></pre>



<h2 class="wp-block-heading">7. 打包 Python3 程序</h2>



<p>将编译好的Python3程序打包到目标环境中：</p>



<pre class="wp-block-code"><code>pwd
/root/0616/Python-3.9.23

make install LDFLAGS="-static" LINKFORSHARED=" " DESTDIR=/root/0616/Python-3.9.23/1</code></pre>



<h2 class="wp-block-heading">8. 部署与复制</h2>



<p>将打包好的文件部署到目标环境：</p>



<pre class="wp-block-code"><code>ls /root/0616/Python-3.9.23/2/usr/local/lib/
libpython3.9.a  pkgconfig       python3.9

rsync -avP /root/0616/Python-3.9.23/2/usr/local/ root@new:/usr/local/
ln -sv /usr/local/bin/python3-static /usr/bin/python3</code></pre>



<h2 class="wp-block-heading">简化部署方案</h2>



<p>更简单的做法是只复制静态编译后的Python文件和相关依赖库到目标环境：</p>



<pre class="wp-block-code"><code>cp ./python3.9.static /target/path/
cp /root/0616/Python-3.9.23/2/usr/local/lib/python3.9 /target/path/</code></pre>



<p>这样，程序就可以在几乎任何发行版上零依赖运行。</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
