英文:
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()
我应该如何实现以下行为:
python
-> 运行这两行代码,然后进入交互式Python 3 shellpython 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:
python
-> run the two lines and drop me into interactive python3 shellpython myscript.py
-> run the two lines, then runmyscript.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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论