编辑:我第一次尝试问这个问题可能有点不专心/措辞不当这里是对我正在尝试做的事情的更好解释:
我正在尝试为运行 python 的整个环境修改打印函数的默认行为,而不必修改正在运行的每个文件。
我正在尝试修饰打印功能(我知道有很多方法可以做到这一点,例如覆盖它,但这并不是我真正要问的问题)所以我可以让它打印出一些调试信息并强制执行它总是冲洗。我是这样做的:
def modify_print(func):
# I made this so that output always gets flushed as it won't by default
# within the environment I'm using, I also wanted it to print out some
# debugging information, doesn't really matter much in the context of this
# question
def modified_print(*args,**kwargs):
return func(f"some debug prefix: ",flush=True,*args,**kwargs)
return modified_print
print = modify_print(print)
print("Hello world") # Prints "some debug prefix Hello World"
但是我想做的是在我的整个应用程序中修改此行为。我知道我可以在每个文件中手动装饰/覆盖/导入打印函数,但是我想知道是否有某种方法可以全局配置我的 python 环境以在任何地方装饰这个函数。我能想到的唯一方法是编辑 python 源代码并构建修改后的版本。
编辑: 这是我想要实现的行为,谢谢 Match 的帮助。 它会在您在 python 环境中调用打印函数的任何地方打印出行号和文件名。这意味着您不必在所有文件中手动导入或覆盖任何内容。
https://gist.github.com/MichaelScript/444cbe5b74dce2c01a151d60b714ac3a
import site
import os
import pathlib
# Big thanks to Match on StackOverflow for helping me with this
# see https://stackoverflow.com/a/48713998/5614280
# This is some cool hackery to overwrite the default functionality of
# the builtin print function within your entire python environment
# to display the file name and the line number as well as always flush
# the output. It works by creating a custom user script and placing it
# within the user's sitepackages file and then overwriting the builtin.
# You can disable this behavior by running python with the '-s' flag.
# We could probably swap this out by reading the text from a python file
# which would make it easier to maintain larger modifications to builtins
# or a set of files to make this more portable or to modify the behavior
# of more builtins for debugging purposes.
customize_script = """
from inspect import getframeinfo,stack
def debug_printer(func):
# I made this so that output always gets flushed as it won't by default
# within the environment I'm running it in. Also it will print the
# file name and line number of where the print occurs
def debug_print(*args,**kwargs):
frame = getframeinfo(stack()[1][0])
return func(f"{frame.filename} : {frame.lineno} ", flush=True,*args,**kwargs)
return debug_print
__builtins__['print'] = debug_printer(print)
"""
# Creating the user site dir if it doesn't already exist and writing our
# custom behavior modifications
pathlib.Path(site.USER_SITE).mkdir(parents=True, exist_ok=True)
custom_file = os.path.join(site.USER_SITE,"usercustomize.py")
with open(custom_file,'w+') as f:
f.write(customize_script)
请您参考如下方法:
您可以使用 site 中的 usercustomize 脚本模块来实现这样的事情。
首先,找出您的用户站点包目录在哪里:
python3 -c "import site; print(site.USER_SITE)"
/home/foo/.local/lib/python3.6/site-packages
接下来,在该目录中,创建一个名为 usercustomize.py 的脚本 - 现在只要运行 python,该脚本就会首先运行。
替换 print 的一种*方法是覆盖 __builtins__ 字典并用新方法替换它 - 类似于:
from functools import partial
old_print = __builtins__['print']
__builtins__['print'] = partial(old_print, "Debug prefix: ", flush=True)
将其放入 usercustomize.py 脚本中,您应该会看到所有 python 脚本从那时起就被覆盖了。您可以通过使用 -s 标志调用 python 来暂时禁用调用此脚本。
*(不确定这是否是正确的方法 - 可能有更好的方法 - 但重点是您可以使用 usercustomize 来交付您选择的任何方法)。
