How to import modules from alternate locations when using Python IDLE?
Question: I've been trying to figure this out for more than 2 days, screening the
internet and the tutorial, but yet I don't have solved my problem. I'm a real
newb and don't yet really know what I'm doing..

Software I use: Mac OS X 10.6 Python v3.2.2 Interactive interpreter (IDLE)

Problem: IDLE's default directory is /Users/ME/Documents/. Files with the
extention .py can only be opened when located in this directory. However, I
made a folder where I would like to save all the .py files etc that have to do
with this software. Currently, IDLE cannot load .py files from the chosen
directory by me.

What I did first was I added to IDLE: import sys.
sys.path.append('Users/Mydir/') sys.path

However, in an already existing thread from 2010 I read sys.path is for the
Interpreter ONLY, and that if I am to change this I need to modify the
PYTHONPATH environment variable:

**PYTHONPATH="/Me/Documents/mydir:$PYTHONPATH" export PYTHONPATH**

However, I'm confused how to use this and cannot find answers to my following
questions: 1) PYTHONPATH (.py?) is already existing on my computer when I
installed the program? If YES, where is it? I cannot find it anywhere. If NO,
I need to create one. But where and what should be the content so that IDLE
can load files from a non-default directory? Should it contain only the words
in bold?

I hope I made my problem clear.

Cheers


Answer: It's not totally clear to me what you mean by `load`. That could mean `Open`
and `Close` files in the IDLE editor. Or it could mean being able to use the
Python `import` statement to load existing Python modules from other files.
I'll assume the latter, that by `load` you mean `import`.

There are two general ways to launch IDLE on Mac OS X. One is from the command
line of a terminal session; if you installed Python 3.2 using the python.org
installers, by default typing `/usr/local/bin/idle3.2` will work. The other
way is by launching `IDLE.app` from `/Applications/Python 3.2`, i.e. by
double-clicking its icon. Because you say the default directory for files is
your `Documents` folder, I'm assuming you are using the second method because
`IDLE.app` sets `Documents` as its current working directory, which becomes
the default directory for *Open*s and *Save*s and is automatically added as
the first directory on Python's `sys.path`, the list of directories that
`Python` uses to search for modules when `import`ing.

If you want to add other directories to `sys.path`, as you've noted you can
use the `PYTHONPATH` environment variable to do so. The standard way to do
this is to add an `export PYTHONPATH=...` definition to a shell startup
script, like `.bash_profile`. However, if you use `IDLE.app`, no shell is
involved so commands in `.bash_profile` have no effect.

While there are ways to modify the environment variables for OS X GUI apps, in
this case, a simpler solution is to use the other method to invoke IDLE, from
the command line of a shell session, using either `/usr/local/bin/idle3.2` or,
if you've run the `Update Shell Profile` command in the `/Applications/Python
3.2` folder (and opened a new terminal session), just `idle3`. Then, a
PYTHONPATH environment variable you set up will be inherited by that IDLE.

BTW, there is no direct way to modify the initial current working directory of
`IDLE.app` from `Documents` other than modifying the code in IDLE. If you
start IDLE from a command line, it inherits the current working directory of
the shell.

[UPDATE] But rather than fooling around with defining `PYTHONPATH`, here is
another even simpler, and probably better, approach that should work with
either `IDLE.app` or the command line `idle`. It takes advantage of [Python
path configuration (`.pth`)
files](http://docs.python.org/py3k/library/site.html) and [user site-package
directories](http://docs.python.org/py3k/library/site.html#site.USER_BASE).
Assuming you are using a standard Python framework build of 3.2 (like from a
python.org installer) on Mac OS X, create a path file for the directory you
want to permanently add to `sys.path`. In a terminal session:

    
    
    mkdir -p ~/Library/Python/3.2/lib/python/site-packages
    cd ~/Library/Python/3.2/lib/python/site-packages
    cat >my_paths.pth <<EOF
    /Users/YOUR_USER_NAME/path/to/your_additional_python_directory_1
    /Users/YOUR_USER_NAME/path/to/your_additional_python_directory_2
    EOF
    

Now, whenever you run that Python 3.2 or IDLE under your user name, the
directories you have added to the `.pth` file will automatically be added to
`sys.path`.

BTW, the exact path location of the user site-packages directory for versions
of Python earlier than 3.2 or 2.7 may be slightly different. Also, on other
Unix-y systems, the default location for the user site-package directory is
`~/.local/lib/python3.2/site-packages`.

