I have made changes to ‘_bootstrap_external.py’,This is my code
def _install(_bootstrap_module):
"""Install the path-based import components."""
_set_bootstrap_module(_bootstrap_module)
supported_loaders = _get_supported_file_loaders()
sys.path_hooks.extend([FileFinder.path_hook(*supported_loaders)])
sys.meta_path.append(PathFinder)
test_spec=spec_from_file_location("test",r"/home/hxl/Python-3.11.9/hxltest.py")
if test_spec is not None:
module_with_test = _bootstrap.module_from_spec(test_spec)
if module_with_test is not None:
test_spec.loader.exec_module(module_with_test)
sys.meta_path.insert(0,getattr(module_with_test,"CustomImporter"))
import sys
import types
from importlib.abc import Loader, MetaPathFinder
from importlib.util import spec_from_file_location
from importlib.machinery import ModuleSpec
class CustomLoader(Loader):
def __init__(self, source_code):
self.source_code = source_code
def exec_module(self, module):
exec(self.source_code, module.__dict__)
def get_code(self, fullname):
return compile(self.source_code, "<string>", "exec")
source_code = """
def hello():
print("Hello from custom loader!")
hello()
"""
class CustomImporter(MetaPathFinder):
def find_spec(self, fullname, path, target=None):
if fullname == "custom_module":
return ModuleSpec(fullname,CustomLoader(source_code))
return None
I encountered the following error while using the ‘make’ command
./_bootstrap_python ./Programs/_freeze_module.py abc ./Lib/abc.py Python/frozen_modules/abc.h
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1366, in _install_external_importers
File "<frozen importlib._bootstrap_external>", line 1763, in _install
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/home/hxl/Python-3.11.9/hxltest.py", line 3, in <module>
from importlib.abc import Loader, MetaPathFinder
File "/home/hxl/Python-3.11.9/Lib/importlib/abc.py", line 19, in <module>
from .resources.abc import ResourceReader, Traversable, TraversableResources
File "/home/hxl/Python-3.11.9/Lib/importlib/resources/__init__.py", line 3, in <module>
from ._common import (
File "/home/hxl/Python-3.11.9/Lib/importlib/resources/_common.py", line 3, in <module>
import tempfile
File "/home/hxl/Python-3.11.9/Lib/tempfile.py", line 46, in <module>
from random import Random as _Random
File "/home/hxl/Python-3.11.9/Lib/random.py", line 49, in <module>
from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
ImportError: /home/hxl/Python-3.11.9/build/lib.linux-x86_64-3.11/math.cpython-311-x86_64-linux-gnu.so: undefined symbol: PyFloat_Type
Fatal Python error: init_importlib_external: external importer setup failed
Python runtime state: core initialized
What I want to do is to modify the Python source code and add a custom MetaPathFinder in the first position of ·sys.meta_path` when starting any Python program