我想知道如何在每次启动 python 解释器时运行一个或多个命令。
有没有办法在 python 中实现这一点,就像 Linux/unix 上的 .bashrc 或 .profile 文件那样?
最佳答案
您可以设置一个环境变量PYTHONSTARTUP
来指向一个文件,该文件包含您希望在所有Python解释器启动时运行的命令。
更多信息可以在 python 文档中找到:https://docs.python.org/2/tutorial/interpreter.html#the-interactive-startup-file
如果您想从当前目录运行附加启动文件或从脚本运行此全局启动文件,还有一些有用的信息:
If you want to read an additional start-up file from the current directory, you can program this in the global start-up file using code like if
os.path.isfile('.pythonrc.py'): execfile('.pythonrc.py')
. If you want to use the startup file in a script, you must do this explicitly in the script:import os filename = os.environ.get('PYTHONSTARTUP') if filename and os.path.isfile(filename): execfile(filename)
关于python - 如何在每次启动Python时运行Python命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25122582/