1

I have python 2.7.2 installed on my Mac. I installed using python-2.7.2-macosx10.6.dmg I have only one instance of python.

When i type in the terminal to find the python path , I get this :

 python Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import os.path >>> os.environ['PYTHONPATH'].split(os.pathsep) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.py", line 23, in __getitem__ raise KeyError(key) KeyError: 'PYTHONPATH' 

It is the one thing missing so I can install Mercurial. I was using Mercurial before and I may have broken something. Do you know how to fix this problem ?

2
  • 1
    Are you sure you want the contents of the environment variable PYTHONPATH specifically, or do you just want to know where Python looks for packages? Commented Feb 16, 2012 at 22:50
  • I'm not sure to understand your question. I display the contents of the environment variable and it raises a KeyError. How to fix PYTHONPATH ? so set the environment variable in the right place . Commented Feb 16, 2012 at 22:59

2 Answers 2

6

You need to modify the sys.path array. It is initialized from the Python defaults and the environment variable "PYTHONPATH": http://docs.python.org/library/sys.html#sys.path

To append your directory the active path:

import sys sys.path.append("/path/to/your/dir") 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Ivan I'll try when I get back home. Can you provide code sample to set the PYTHONPATH ?
I've added sample to my response
Not sure why @roy-m-j feels that there needs to be a semicolon there, but Python does not require this to indicate a line change.
@Lars some might even consider it un-pythonic to have a semicolon there. I removed it
1

Here's another useful tip:

Look under your ~/.local folder. There may be a homebrew.pth (or *.pth) file under ~/.local/lib/ which may cause issue by overriding the python path.

If unsetting $PYTHONPATH doesn't fix your issue, try removing the:

~/.local folder.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.