武器商人@Pythonのブログ

Pythonを好んで使っているプログラマです。Pythonこそが最強のプログラミング言語だと思っています。Pythonは使いやすいです。Pythonは書きやすいです。Pythonは読みやすいです。Pythonはパワフルです。Pythonは一貫性があります。Pythonが好きです。

IPythonの起動時にモジュールをインポート

IPythonの起動時にモジュールをインポートしたい

毎回おまじないのように、モジュールをインポートするのは面倒臭い。起動時に自動的に読み込んでくれればいいのに。と思ったときには、プロファイルを編集してあげれば実現可能。
私の場合、oneshotlife_tomというプロファイルを作成したので、プロファイルパスは以下となる。

C:\Users\oneshot\.ipython\profile_oneshotlife_tom

そこにある"ipython_config.py"を編集してあげれば良い。

変更点

変更前

#c.InteractiveShellApp.exec_lines = []

変更後

c.InteractiveShellApp.exec_lines = [
    'import sys',
    'import os'    
]

動作確認結果

C:\Users\oneshot\Desktop>ipython --profile='oneshotlife_tom'
Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.

IPython 4.1.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

IPython profile: oneshotlife_tom

>>>sys.
sys.api_version           sys.float_repr_style      sys.platform
sys.argv                  sys.getcheckinterval      sys.prefix
sys.builtin_module_names  sys.getdefaultencoding    sys.ps1
sys.byteorder             sys.getfilesystemencoding sys.ps2
sys.call_tracing          sys.getprofile            sys.ps3
sys.callstats             sys.getrecursionlimit     sys.py3kwarning
sys.copyright             sys.getrefcount           sys.setcheckinterval
sys.displayhook           sys.getsizeof             sys.setprofile
sys.dllhandle             sys.gettrace              sys.setrecursionlimit
sys.dont_write_bytecode   sys.getwindowsversion     sys.settrace
sys.exc_clear             sys.hexversion            sys.stderr
sys.exc_info              sys.long_info             sys.stdin
sys.exc_type              sys.maxint                sys.stdout
sys.excepthook            sys.maxsize               sys.subversion
sys.exec_prefix           sys.maxunicode            sys.version
sys.executable            sys.meta_path             sys.version_info
sys.exit                  sys.modules               sys.warnoptions
sys.exitfunc              sys.path                  sys.winver
sys.flags                 sys.path_hooks
sys.float_info            sys.path_importer_cache

読み込まれている!

Windows10でpipのバージョンアップ

pipを使っていると古いバージョンを使っているとのワーニングが出ていることに気付いた。

pip : You are using pip version 7.1.0, however version 9.0.0 is available.

いちいち言われるのは面倒臭いからpipをバージョンアップしようと思った。

pipのインストールは以下のコマンドで実行出来る。
ところが、アクセスが拒否されるとのメッセージが!!!

C:\Users\oneshot>python -m pip install --upgrade pip
You are using pip version 7.1.0, however version 9.0.0 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
Collecting pip
  Using cached pip-9.0.0-py2.py3-none-any.whl
Installing collected packages: pip
  Found existing installation: pip 7.1.0
    Uninstalling pip-7.1.0:
Exception:
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\pip\basecommand.py", line 223, in main
    status = self.run(options, args)
  File "C:\Python27\lib\site-packages\pip\commands\install.py", line 299, in run
    root=options.root_path,
  File "C:\Python27\lib\site-packages\pip\req\req_set.py", line 640, in install
    requirement.uninstall(auto_confirm=True)
  File "C:\Python27\lib\site-packages\pip\req\req_install.py", line 726, in uninstall
    paths_to_remove.remove(auto_confirm)
  File "C:\Python27\lib\site-packages\pip\req\req_uninstall.py", line 125, in remove
    renames(path, new_path)
  File "C:\Python27\lib\site-packages\pip\utils\__init__.py", line 314, in renames
    shutil.move(old, new)
  File "C:\Python27\lib\shutil.py", line 303, in move
    os.unlink(src)
WindowsError: [Error 5] アクセスが拒否されました。: 'c:\\python27\\lib\\site-packages\\pip-7.1.0.dist-info\\description.rst'

どうやらWindows10で普通にコマンドプロンプトを立ち上げると、管理者権限が付いていないようだ。Linuxで言うところのsudoとか付ければいいんだろうけど、Windows10にそのようなコマンドがあるかはわからない。コマンドプロンプトを管理者権限で開いて実行してみたところ上手く行った。

C:\WINDOWS\system32>python -m pip install --upgrade pip
You are using pip version 7.1.0, however version 9.0.0 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
Collecting pip
  Using cached pip-9.0.0-py2.py3-none-any.whl
Installing collected packages: pip
  Found existing installation: pip 7.1.0
    Uninstalling pip-7.1.0:
      Successfully uninstalled pip-7.1.0
Successfully installed pip-9.0.0

面倒臭くなったものだ。