尝试在我的IPython配置文件中添加一些导入,这样当我在Spyder IDE中打开内核时,他们总是被加载。 Spyder有一个Qt接口(我想??),所以我(a)检查以确保我在终端(OSX)中使用ipython locate
命令在配置文件的正确目录中,以及(b)放置我的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
,我就不会遇到任何问题。
建议?
谢谢!
答案 0 :(得分:1)
Spyder是否使用QT接口不应与您要修改的哪个IPython配置文件相关。您选择修改的那个,ipython_qtconsole_config.py
是启动 IPython的 QT控制台时加载的配置文件,例如使用命令行命令
user@system:~$ ipython qtconsole
(我需要更新pyzmq
才能使其发挥作用。)
如果 Spyder 维护一个正在运行的IPython内核,并且只管理如何为您显示,那么Spyder可能只是维护一个常规的IPython会话,在这种情况下,您希望您的配置设置进入文件ipython_config.py
位于找到ipython_qtconsole_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和笔记本,所以配置文件彼此之间有意义的不同,只是不基于我想在启动时导入的模块。