如何在每次使用Python(交互式和执行脚本时)时自动运行特定的导入?

huangapple go评论66阅读模式
英文:

How do I automatically run a specific import every time I use python (both interactively and when executing scripts)?

问题

Sure, here is the translated content:

我是rich的漂亮打印选项的忠实粉丝。

因此,我想每次使用Python(在任何其他操作之前)都自动运行以下两行代码:

from rich import pretty
pretty.install()

我应该如何实现以下行为:

  1. python -> 运行这两行代码,然后进入交互式Python 3 shell
  2. python myscript.py -> 运行这两行代码,然后运行myscript.py

我已经将这两行代码放入了一个名为startup.py的文件中,并在我的~/.bashrc中创建了一个别名:

alias python='python3 -i /path/to/startup.py'

这在我使用Python标准Shell时非常有效,只需调用python(情况1)。但是,我不能使用这个别名来运行脚本,例如python myscript.py(情况2)。

我正在使用Python 3.10.6,在Ubuntu 22.04上。

英文:

I am a big fan of the pretty printing option of rich.

Thus, I want to automatically run the lines

from rich import pretty
pretty.install()

every time I use python (before anything else).

How can I achieve the following behavior:

  1. python -> run the two lines and drop me into interactive python3 shell
  2. python myscript.py -> run the two lines, then run myscript.py

I have already put the two lines into a startup.py file and created an alias in my ~/.bashrc:

alias python='python3 -i /path/to/startup.py'

which works great when I am using the python standard shell by just calling python (case 1). However, I cannot use the alias to run scripts, for example with python myscript.py (case 2).

I am using Python 3.10.6 on Ubuntu 22.04.

答案1

得分: 2

I have actually found a solution in the meanwhile.

如果您只想实现情况#1,您应该使用PYTHONSTARTUP环境变量,通过在您的〜/.bashrc中设置它:

export PYTHONSTARTUP="/path/to/startup.py"

要实现情况#1和情况#2,您应该将启动文件命名为usercustomize.py并将其放置在USER_SITE目录中,您可以使用python -m site找到它。这是修改site.py行为的推荐方法。有关更多详细信息,请参阅此问题的被接受答案

第二种方法有一个缺点:与PYTHONSTARTUP解决方案相比,usercustomize.py中的导入在您进入交互式shell或执行脚本时会被忘记。例如,pretty.install()有效,而from rich import print不会覆盖默认的打印函数,因为它只是一个导入。如果您想在交互式shell中使用默认导入(例如import numpy as np,因为您经常使用它),您必须同时使用usercustomize.py和PYTHONSTARTUP。我尚未找到在执行脚本时保留导入的解决方案。

感谢@pynexj指出我以前解决方案中的缺陷。

英文:

I have actually found a solution in the meanwhile.

If you only want to achieve case #1, you should make use of the PYTHONSTARTUP environment variable, by setting it in your ~/.bashrc:

export PYTHONSTARTUP="/path/to/startup.py"

To achieve cases #1 and #2, you should name your startup file usercustomize.py and place it in your USER_SITE directory, which you can find with python -m site. This is the recommended way of modifying the behavior of site.py. More details in the accepted answer of this question.

The second approach has one downside: Compared to the PYTHONSTARTUP solution, imports being done in usercustomize.py are forgotten once you are dropped into the interactive shell or your script is being executed. For example, pretty.install() works, while from rich import print would not overwrite the default print function, as it is just an import. If you want to use default imports in the interactive shell (e.g. import numpy as np, because you use it so frequently), you have to use both, usercustomize.py and PYTHONSTARTUP. I have not yet found a solution that preserves imports while executing a script.

Thanks to @pynexj for pointing out a flaw in my previous solution.

huangapple
  • 本文由 发表于 2023年5月10日 20:28:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定