Python
virtualenv
direnv

direnvでpython3.4を.envrcに記述しallowしたらsyntaxエラーが出ました

More than 1 year has passed since last update.

Macのローカルでdirenv allowしたらsyntaxエラーが出ました。

wakana-no-MacBook-Pro:Documents wakanayoshizawa$ cd test/
direnv: loading .envrc
Running virtualenv with interpreter /Users/wakanayoshizawa/.pythonz/pythons/CPython-3.4.3/bin/python3
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 37, in <module>
    import ConfigParser
ImportError: No module named 'ConfigParser'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 39, in <module>
    import configparser as ConfigParser
  File "/usr/local/lib/python2.7/site-packages/configparser.py", line 397
    _KEYCRE = re.compile(ur"%\(([^)]+)\)s")
                                         ^
SyntaxError: invalid syntax
direnv: error exit status 1

configparser.pyの397行目を見てみると、

394:try:
395:    import ConfigParser
396:except ImportError:
397:    import configparser as ConfigParser

このページを見ると、どうやらPython3.4でPython2.7の環境を読み込んでいるため、互換性が必要とのことでした。
virtualenvwrapperが古いか、入ってない可能性があるため以下のコマンドを叩きました。

sudo pip install -U virtualenvwrapper
wakana-no-MacBook-Pro:Documents wakanayoshizawa$ sudo pip install -U virtualenvwrapper
Password:
The directory '/Users/wakanayoshizawa/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/wakanayoshizawa/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already up-to-date: virtualenvwrapper in /usr/local/lib/python2.7/site-packages
Collecting virtualenv (from virtualenvwrapper)
  Downloading virtualenv-14.0.6-py2.py3-none-any.whl (1.8MB)
    100% |████████████████████████████████| 1.8MB 274kB/s 
Requirement already up-to-date: virtualenv-clone in /usr/local/lib/python2.7/site-packages (from virtualenvwrapper)
Collecting stevedore (from virtualenvwrapper)
  Downloading stevedore-1.11.0-py2.py3-none-any.whl
Collecting pbr>=1.6 (from stevedore->virtualenvwrapper)
  Downloading pbr-1.8.1-py2.py3-none-any.whl (89kB)
    100% |████████████████████████████████| 90kB 2.5MB/s 
Requirement already up-to-date: six>=1.9.0 in /usr/local/lib/python2.7/site-packages (from stevedore->virtualenvwrapper)
Installing collected packages: virtualenv, pbr, stevedore
  Found existing installation: virtualenv 13.1.2
    Uninstalling virtualenv-13.1.2:
      Successfully uninstalled virtualenv-13.1.2
  Found existing installation: pbr 1.8.0
    Uninstalling pbr-1.8.0:
      Successfully uninstalled pbr-1.8.0
  Found existing installation: stevedore 1.8.0
    Uninstalling stevedore-1.8.0:
      Successfully uninstalled stevedore-1.8.0
Successfully installed pbr-1.8.1 stevedore-1.11.0 virtualenv-14.0.6

もう一度ディレクトリに入って.envrcを読み込んでみます。

wakana-no-MacBook-Pro:Documents wakanayoshizawa$ cd test/
direnv: loading .envrc
Running virtualenv with interpreter /Users/wakanayoshizawa/.pythonz/pythons/CPython-3.4.3/bin/python3
Using base prefix '/Users/wakanayoshizawa/.pythonz/pythons/CPython-3.4.3'
New python executable in /Users/wakanayoshizawa/Documents/test/.direnv/python-3.4.3/bin/python3
Also creating executable in /Users/wakanayoshizawa/Documents/test/.direnv/python-3.4.3/bin/python
Installing setuptools, pip, wheel...done.
direnv: export +VIRTUAL_ENV ~PATH

ちゃんとインストール出来ました\(^o^)/