Search code examples
pythonzshcd

Why shell returned a Python error traceback when I `cd` into python-skeleton dir


I installed Pycharm via HomeBrew cask on my OS X 10.10. I heard python-skeleton is something useful so cded into it to find what does it have. But every time I type cd /opt/homebrew-cask/Caskroom/pycharm/4.5/PyCharm.app/Contents/helpers/python-skeletons/ gets this traceback:

Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 548, in <module>
    main()
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 527, in main
    known_paths = removeduppaths()
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 110, in removeduppaths
    dir, dircase = makepath(dir)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 80, in makepath
    dir = os.path.join(*paths)
AttributeError: 'module' object has no attribute 'path'

Anything else is alright, it only appears when I cd into the special dir.

I'm using zsh and oh-my-zsh. After bi-searching as @skyline75489 mentioned, I find out it's something wrong with the autojump config in my .zshrc file:

[[ -s $(brew --prefix)/etc/profile.d/autojump.sh ]] && . $(brew --prefix)/etc/profile.d/autojump.sh

Solution

  • Simple, it's exactly what the exception says: os module has no attribute path. python-skeletons has a package called os, and the os package in the current working directory when you are in blahblah/python-skeletons is shielding the os package from standard library. This is because present working directory comes before library paths in sys.path, the list of paths where Python look for modules.

    In general it's rather bad practice to have a module or package that has the same name as an STL module or package (unless it is designed to be a drop-in replacement), but in this case it's there, and there's nothing you can do about it.

    Just don't j from or to that directory. If your cd is shielded by a function or alias for autojump, then use builtin cd instead.