python - 如何在每次启动Python时运行Python命令

标签 python startup

我想知道如何在每次启动 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/

相关文章:

python - 在 Python2 中从 sys.stdin 制作 io.BufferedReader

python - 如何使用 Pandas 中另一列的 unicode 文本创建新列?

c# - 在启动时以管理员身份运行

installation - 为什么InstallShield 创建的启动快捷方式的自动运行信息不正确?

python - 在 SPSS 中使用 Python 使用循环仅重新编码某些变量?

python - 在 Python 中复制和写入 XML 节点

Python:如何在每次调用时更新变量的值?

asp.net - Windows Azure 启动任务未触发

c# - Entity Framework Code First 30+ 秒启动时间

jenkins - 迁移 Jenkins 后禁用所有构建