Spyder IDE 的 IPython 启动配置

3
尝试将一些导入添加到我的IPython配置文件中,以便在Spyder IDE中打开内核时它们始终被加载。Spyder具有Qt界面(我想是这样的?),所以我首先使用终端(OSX)中的ipython locate命令检查了我的配置文件所在的目录,然后将以下代码放置在ipython_qtconsole_config.py文件中:
c.IPythonQtConsoleApp.exec_lines = ["import pandas as pd", 
                                "pd.set_option('io.hdf.default_format', 'table')",
                                "pd.set_option('mode.chained_assignment','raise')",
                                "from __future__ import division, print_function"]

但是当我打开一个新窗口并输入 pd.__version__ 时,会出现 NameError: name 'pd' is not defined 错误。

编辑:如果我从终端运行 ipython qtconsole,就没有任何问题。

有什么建议吗?

谢谢!

1个回答

1
无论Spyder是否使用QT界面,都不应该与您想要修改的IPython配置文件有关。您选择修改的ipython_qtconsole_config.py是在启动IPython的 QT控制台时加载的配置文件,例如使用命令行命令时。
user@system:~$ ipython qtconsole

我需要更新pyzmq才能使其正常工作。
如果Spyder维护一个正在运行的IPython内核并仅管理如何为您显示它,那么Spyder可能只是维护一个常规的IPython会话,在这种情况下,您希望将配置设置放入与找到ipython_qtconsole_config.py相同的目录中的文件ipython_config.py中。
我以稍微不同的方式管理这个。在ipython_config.py中,我的前几行看起来像这样:
# Configuration file for ipython.
from os.path import join as pjoin
from IPython.utils.path import get_ipython_dir

c = get_config()
c.InteractiveShellApp.exec_files = [
    pjoin(get_ipython_dir(), "profile_default", "launch.py")
]

这段代码的作用是为我获取IPython配置目录,添加profile_default子目录,然后添加名称为launch.py的文件。我创建了这个文件,以便在启动时执行/加载任何我想要的内容。例如,下面是launch.py文件的第一部分:
"""
IPython launch script
Author: Ely M. Spears
"""

import re
import os
import abc
import sys
import mock
import time
import types
import pandas
import inspect
import cPickle
import unittest
import operator
import warnings
import datetime
import dateutil
import calendar
import copy_reg
import itertools
import contextlib
import collections
import numpy as np
import scipy as sp
import scipy.stats as st
import scipy.weave as weave
import multiprocessing as mp
from IPython.core.magic import (
    Magics,
    register_line_magic,
    register_cell_magic,
    register_line_cell_magic
)
from dateutil.relativedelta import relativedelta as drr


###########################
# Pickle/Unpickle methods #
###########################

# See explanation at:
# < http://bytes.com/topic/python/answers/
#   552476-why-cant-you-pickle-instancemethods >
def _pickle_method(method):
    func_name = method.im_func.__name__
    obj = method.im_self
    cls = method.im_class
    return _unpickle_method, (func_name, obj, cls)

def _unpickle_method(func_name, obj, cls):
    for cls in cls.mro():
        try:
            func = cls.__dict__[func_name]
        except KeyError:
            pass
        else:
            break
    return func.__get__(obj, cls)

copy_reg.pickle(types.MethodType, _pickle_method, _unpickle_method)

#############
# Utilities #
#############
def interface_methods(*methods):
    """
    Class decorator that can decorate an abstract base class with method names
    that must be checked in order for isinstance or issubclass to return True.
    """
    def decorator(Base):
        def __subclasshook__(Class, Subclass):
            if Class is Base:
                all_ancestor_attrs = [ancestor_class.__dict__.keys()
                                      for ancestor_class in Subclass.__mro__]
                if all(method in all_ancestor_attrs for method in methods):
                    return True
            return NotImplemented
        Base.__subclasshook__ = classmethod(__subclasshook__)
        return Base

def interface(*attributes):
    """
    Class decorator checking for any kind of attributes, not just methods.

    Usage:

    @interface(('foo', 'bar', 'baz))
    class Blah
       pass

    Now, new classes will be treated as if they are subclasses of Blah, and 
    instances will be treated instances of Blah, provided they possess the
    attributes 'foo', 'bar', and 'baz'.
    """
    def decorator(Base):
        def checker(Other):
            return all(hasattr(Other, a) for a in attributes)

        def __subclasshook__(cls, Other):
            if checker(Other):
                return True
            return NotImplemented

        def __instancecheck__(cls, Other):
            return checker(Other)

        Base.__metaclass__.__subclasshook__ = classmethod(__subclasshook__)
        Base.__metaclass__.__instancecheck__ = classmethod(__instancecheck__)
        return Base
    return decorator

可能有数十个辅助函数、我认为很酷的代码片段以及想要玩耍的东西等等。我还定义了一些随机生成的玩具数据集,如NumPy数组和Pandas DataFrames,这样当我想要探索某些一次性Pandas语法或其他内容时,就有一些玩具数据可供使用。

另一个好处是因为将自定义导入、函数定义等分离出来,所以如果我想要在笔记本电脑和/或qt控制台中加载相同的内容,我只需添加同样的代码来执行文件launch.py,并且我可以仅在launch.py中进行更改,而无需手动将其迁移到三个配置文件中的每个文件。

我还取消了几个不同的设置,特别是对于纯IPython和笔记本电脚本,所以配置文件在意义上互不相同,但不基于我想要在启动时导入哪些模块。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接