python3.3以降では、mock は標準ライブラリになっています。
python3.3以前だと、python の pip でインストールする必要があります。
Mac OS El Caption で、python2.7、python3.6 の双方で mock を動作させるのに一手間必要でした。
一手間について記載します。


参考


実施したこと

pip インストール

  • コマンド

    pip install mock
    
    コマンドを実行したところ、以下のエラーが発生しました。

  • エラー内容

    Installing collected packages: six, funcsigs, pbr, mock
      Found existing installation: six 1.4.1
        DEPRECATION: Uninstalling a distutils installed project (six) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
        Uninstalling six-1.4.1:
    Exception:
    Traceback (most recent call last):
      File "/Library/Python/2.7/site-packages/pip/basecommand.py", line 215, in main
        status = self.run(options, args)
      File "/Library/Python/2.7/site-packages/pip/commands/install.py", line 317, in run
        prefix=options.prefix_path,
      File "/Library/Python/2.7/site-packages/pip/req/req_set.py", line 736, in install
        requirement.uninstall(auto_confirm=True)
      File "/Library/Python/2.7/site-packages/pip/req/req_install.py", line 742, in uninstall
        paths_to_remove.remove(auto_confirm)
      File "/Library/Python/2.7/site-packages/pip/req/req_uninstall.py", line 115, in remove
        renames(path, new_path)
      File "/Library/Python/2.7/site-packages/pip/utils/__init__.py", line 267, in renames
        shutil.move(old, new)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
        copy2(src, real_dst)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 131, in copy2
        copystat(src, dst)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 103, in copystat
        os.chflags(dst, st.st_flags)
    OSError: [Errno 1] Operation not permitted: '/var/folders/x7/8dsf6p5x2vb0cgxf8x1w21jm0000gn/T/pip-KZoyPu-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'
    

sudo 付きで pip インストール

権限がないのかと思ったので、sudo をつけて pip を実行しました。

six を除外して pip インストール

--ignore-installed six付与し、pip コマンドを実行しました。

  • コマンド

    sudo pip install mock --upgrade --ignore-installed six
    

  • 実行結果

    Successfully installed funcsigs-1.0.2 mock-2.0.0 pbr-0.10.7 six-1.4.1
    
    インストール成功しました。

テストを実行

確認のため、テスト実行してみます。

  • コマンド

    python runtests.py
    

  • 実行結果
    以下、エラーが発生しました。

      File "/Library/Python/2.7/site-packages/mock/__init__.py", line 2, in <module>
        import mock.mock as _mock
      File "/Library/Python/2.7/site-packages/mock/mock.py", line 68, in <module>
        from six import wraps
    ImportError: cannot import name wraps
    

エラーの解決方法

Web検索したところ、Stack OverflowQ&Aを見つけました。 six 古いバージョンのバグ? でパスを通さないと動かないようでしたので、
.bash_profile に以下の記述を追加しました。
console export PYTHONPATH="/Library/Python/2.7/site-packages:$PYTHONPATH"

訂正

PYTHONPATH を追加する方法だと、python2は動作しますが、python3 実行時にエラーとなりました。
このため、six 自体のversion を version up して対処しました。
手順を記載できていなかったですが、以下を参考にupgrade は実施できるかと思います。

以下、upgrade実行後の sixversion になります。

pip list | grep six
-------------------------------
six (1.10.0)
-------------------------------

テストの再実行

再度テストを実行します。

python runtests.py
---------------------------------------
Ran 1 test in 0.014s

OK
エラーが発生することなく、動作することを確認しました。

以上です。

コメント