adding a directory to sys.path

Discussion in 'Python' started by John Guenther, Nov 23, 2009.

  1. This is Mac related. I am running snow leopard. I am using Python 2.6.3.

    I had a lot of difficulty figuring out how to add a directory to sys.path that would be there every time I launched Idle. I have a directory called PythonPrograms in my Documents folder. When I installed Python it had '/Users/johnguenther/Documents' as sys.path[0]. I tried setting PYTHONPATH and this worked for running Python from the bash shell but only until I exited the shell. PYTHONPATH had to be reset each time I restarted the shell. And it had no effect on sys.path when I ran Idle.

    I tried putting files with names ending with .pth in various directories all of which were already in sys.path. In this file I would put the path name of the directory, i.e., '/Users/johnguenter/Documents/PythonPrograms'. This had absolutely no effect.

    I searched the web for help and found nothing that worked.

    Finally I looked at a file that had been installed when I installed wxPython. I modified that file and now the directory I want is always part of sys.path when I start idle.

    Since I had so much trouble finding an answer to the question "How do I permanently change sys.path?", I thought I might post this message.

    I created a file called myconfig.pth

    In this file I included one line:

    import site; site.addsitedir('/Users/johnguenther/Documents/PythonPrograms')

    I placed the file in /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages.
    John Guenther, Nov 23, 2009
    1. Advertisements

  2. John Guenther

    Paul Miller Guest


    For a comprehensive discussion of how to modify sys.path, see

    If you really just need to modify sys.path for one particular user, what
    I would do is add the line

    export PYTHONSTARTUP="/home/my_user/python/"

    to my .bashrc. Then, in the file /home/my_user/python/,
    include the following code:

    import sys

    sys.path.append ("/home/my_user/some_dir")

    This will add the directory /home/my_user/some_dir to sys.path every time
    you start up Python.

    The reason I like using PYTHONSTARTUP is that you can do other sorts of
    customizations in that file, as well. For example, I hate not having any
    way to clear the screen when I'm typing at the Python prompt, so I added
    this code to my $PYTHONSTARTUP file:

    import os

    def clear():
    os.system ('clear')

    Hope that all helps!
    Paul Miller, Nov 24, 2009
    1. Advertisements

  3. John Guenther


    Jan 8, 2012
    Likes Received:
    I just attempted the following:
    The problem is, it didn't work on my Debian Laptop. The sys.path used by IDLE refuses to use the paths that I specified. Anybody have any ideas?
    WanderingOak, Jan 8, 2012
  4. John Guenther


    Feb 15, 2012
    Likes Received:
    This worked for me in Windows

    Add an environmental variable PYTHONPATH

    MyComputer -> Properties -> Advanced System Settings -> Environmental Variables

    New ->
    Value C:\Python32\sample

    Restart the IDLE

    Hope this works for everyone using Python on Windows.

    ram_g, Feb 15, 2012
  5. John Guenther


    Jan 8, 2012
    Likes Received:
    Any idea how to do that in Debian Linux?
    WanderingOak, Feb 15, 2012
  6. John Guenther


    Jun 28, 2017
    Likes Received:
    I needed IDLE to be able to find the standard pip installed modules
    so I could import things like numpy, or matplotlib, etc., so this is what I did:

    sudo gedit /usr/local/lib/python3.6/

    change the line:



    USER_SITE = '/usr/share'
    UoPTucson, Jun 28, 2017
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.