Help: PYTHONPATH vs. system PATH

Discussion in 'Mac Programming' started by triple-h, Aug 30, 2011.

  1. triple-h macrumors newbie

    Joined:
    Aug 30, 2011
    #1
    Hi,

    I'm trying to code in Python 2.7 on OS X Snow Leopard. When I tried to get libraries like opencv and pyqt4, I faced so many problems. First, my system would mix up between my Python 2.7 installation and the default one that comes with OS X.

    Also, I faced problems importing opencv because Python wouldn't recognize it. I solved the problem later by pasting some code that exported a variable to PYTHONPATH, but I didn't understand what really happened.

    Now, I'm facing similar problems with pyqt4 whenever I try to execute the command pyuic4 from the terminal [it says "command not found"]

    I tried installing via Macports but I still got the same problems. I'm pretty sure it is because I need to modify PYTHONPATH and/or the system PATH in .bash_profile, but I'm clueless. I have no idea what to do because I don't understand what these things are about.

    Can you please help me understand how to get things right?
     
  2. willieva macrumors 6502

    Joined:
    Mar 12, 2010
    #2
    I highly recommend getting a book on shell programming and reading the first few chapters. Learning The bash Shell by Newham and Rosenblatt is a decent one. There are many others.

    Your PATH variable controls what executables are available from the command line without using their absolute paths. If you type:
    env | grep PATH
    you will see what directories are in your path.

    To add the pyuic4 command to your environment, you need to find where the command is then add it's path to your PATH environment variable. So say the pyuic4 command is in /Users/you/local/bin
    you would issue the command:
    export PATH=/Users/you/local/bin:$PATH

    You could put that at the end of your .bash_profile file and subsequent shells would also see it.

    The PYTHONPATH variable is used by python to find packages. If you know what it was set to when you got things working, you could add the same line to your .bash_profile file.
    export PYTHONPATH=whatevermadeitwork
     
  3. triple-h thread starter macrumors newbie

    Joined:
    Aug 30, 2011
    #3
    Thank you so much! Your explanation is great and simple.

    I opened my .bash_profile by using this command:
    Code:
    sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit ~/.bash_profile
    
    then I added this line:
    Code:
    export PATH=/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin:$PATH
    
    I then restarted the terminal, and pyuic4 was working!

    [Note: I installed it via Macports, which is why the path is /opt/local/etc...]
    [Note#2: This also solved the issues I had with other scripts, like py2applet, since they're located in the same dir]

    Thank you so much! :D:D:D
     
  4. willieva macrumors 6502

    Joined:
    Mar 12, 2010
    #4
    Glad I could be of help.

    I'd recommend against using the sudo command until you learn a bit more. It allows you to run commands as a different user, in this case as root. Root is the supreme user on a unix box, and as such can do lots of damage.

    You don't need to use sudo to edit files in your home directory(that's what the ~ in ~/.bash_profile means), unless your permissions are completely screwed up. Which they could get to be if you keep doing this with sudo.
     
  5. triple-h thread starter macrumors newbie

    Joined:
    Aug 30, 2011
    #5
    Yeah you're absolutely right. I can run it without sudo with no errors, so I'm fine. No more sudo for me I guess :p
     

Share This Page