簡體   English   中英

spyder IDE的ipython啟動配置

[英]ipython startup config for spyder IDE

試圖在我的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 ,我沒有任何問題。

建議?

謝謝!

Spyder是否使用QT接口不應與您要修改的哪個IPython配置文件相關。 您選擇修改的ipython_qtconsole_config.pyipython_qtconsole_config.py是啟動IPython的 QT控制台時加載的配置文件,例如使用命令行命令

user@system:~$ ipython qtconsole

(我需要更新pyzmq才能使用。)

如果Spyder維護一個正在運行的IPython內核並且只管理如何為你顯示,那么Spyder可能只是維護一個常規的IPython會話,在這種情況下你希望你的配置設置進入你的同一目錄下的ipython_config.py文件找到了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和筆記本,所以配置文件彼此有意義的不同,只是沒有基於我想在啟動時導入的模塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM