如何在Python启动时执行任意代码?修改BuiltinImporter无效
Python启动自动执行代码的可行方案
问题背景
用户尝试修改Python 3.10中_frozen_importlib的BuiltinImporter类,在/data/data/com.termux/files/usr/lib/python3.10/importlib/_bootstrap.py里添加test属性后,重启Python依然无法读取该属性,核心需求是实现Python启动时自动执行指定代码(如print(1))。
用户操作过程:
.../python3.10/importlib $ python Python 3.10.5 (main, Jun 7 2022, 03:53:14) [Clang 12.0.8 (https://android.googlesource.com/toolchain/llvm-project c935d99d7 on linux Type "help", "copyright", "credits" or "license" for more information. >>> globals() {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>} >>> __loader__ <class '_frozen_importlib.BuiltinImporter'> >>> __loader__.test Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: type object 'BuiltinImporter' has no attribute 'test' >>> __import__('_frozen_importlib').__file__ '/data/data/com.termux/files/usr/lib/python3.10/importlib/_bootstrap.py' >>> [17]+ Stopped python .../python3.10/importlib $ cd '/data/data/com.termux/files/usr/lib/python3.10/importlib/' .../python3.10/importlib $ grep -rn "class BuiltinImporter" _bootstrap.py:724:class BuiltinImporter: .../python3.10/importlib $ echo "i will add 'test' attribute to 'BuiltinImporter' class" i will add 'test' attribute to 'BuiltinImporter' class .../python3.10/importlib $ nano _bootstrap.py .../python3.10/importlib $ grep -rn "test=" _bootstrap.py:731: test= "Hi!" .../python3.10/importlib $ python Python 3.10.5 (main, Jun 7 2022, 03:53:14) [Clang 12.0.8 (https://android.googlesource.com/toolchain/llvm-project c935d99d7 on linux Type "help", "copyright", "credits" or "license" for more information. >>> __loader__ <class '_frozen_importlib.BuiltinImporter'> >>> __loader__.test Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: type object 'BuiltinImporter' has no attribute 'test' >>> # how can make this work? >>>
为什么修改_bootstrap.py无效?
_frozen_importlib是Python解释器内置的冻结模块,它的代码已经被编译进Python的二进制执行文件中,运行时加载的是这个预编译版本,而不是你修改的_bootstrap.py文件,所以修改源码不会生效。
实现Python启动自动执行代码的可行方法
1. 利用PYTHONSTARTUP环境变量(仅交互式解释器)
- 创建启动脚本,比如
~/.python_startup.py,写入需要执行的代码:print(1) # 可添加其他初始化代码,比如导入常用模块 import os import sys - 在shell配置文件(如
~/.bashrc、~/.zshrc或Termux的~/.bashrc)中添加环境变量:export PYTHONSTARTUP=~/.python_startup.py - 重启shell或执行
source ~/.bashrc使配置生效,之后每次启动交互式Python都会自动执行该脚本。
2. 使用site模块自定义脚本(全局生效)
Python的site模块会自动加载特定目录下的自定义脚本,适用于所有Python运行场景(除非用-S参数跳过site模块):
- 找到你的Python环境的
site-packages目录,可通过以下命令查看:import site print(site.getsitepackages()) - 在该目录下创建
sitecustomize.py文件,写入执行代码:print(1) - 之后无论启动交互式解释器还是运行Python脚本,都会自动执行
sitecustomize.py中的代码。
3. 启动时直接指定代码(临时/特定场景)
如果仅需要临时生效或针对特定启动场景,可直接在启动命令中指定代码:
- 直接执行代码并进入交互式环境:
python -c "print(1); exec(open('/dev/stdin').read())" - 也可以将命令设为shell别名,方便使用:
之后输入alias py='python -c "print(1); exec(open(\"/dev/stdin\").read())"'py即可自动执行print(1)并进入交互式环境。
内容的提问来源于stack exchange,提问作者Mr. Nom4ik
相关产品推荐
相关产品推荐

