python - 检测 .pythonrc.py 中的可执行文件(python?ipython?)

标签 python ipython python-2.x

我意识到我有一个相当大的~/.pythonrc.py,特别是有助于 pretty-print 和制表符完成。

但是我现在经常使用 ipython,并且 .pythonrc.py 中执行的一些命令让我很烦,并且在启动时减慢了速度。

与其删除文件或将其注释掉,更好的是根据我是否启动 python 还是 ipython 有条件地执行脚本。

我该怎么做?
如何从.pythonrc.py内部检测启动脚本?

注意:sys.argv.pythonrc.py 中使用时没有帮助

最佳答案

我认为您可以使用 Detecting when a python script is being run interactively in ipython 中使用的响应,即在你的pythonrc文件中,检测变量__IPYTHON__是否存在:

def in_ipython():
    try:
        __IPYTHON__
    except NameError:
        return False
    else:
        return True

关于python - 检测 .pythonrc.py 中的可执行文件(python?ipython?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27635907/

相关文章:

python - 读入 python 时,Google Trends API 不是很好的 json

python - Jupyter Notebook 在 Pycharm 中无法完全运行

mercurial - IPython、Sage、依赖项、Anaconda 和包集成?

Python SQL游标给出不同的结果?

python - Python默认字符串编码

python - 比较字典列表并合并到一个python

python - 使用 Mechanize/ Selenium 抓取网站时缺少 html 中的内容

python - 使用函数接受不是标识符的 kwargs 关键字参数是否安全?

python - 使用 Swig 和 distutils 为 Python 3 构建扩展

python - 导入在 Python 中有效,但在 IPython 中无效