I don't want to save all of my python files in the default directory, but if I save them elsewhere, python can't find the path to any modules I might be using. How can I change this to work?

what I understand is, custom module you import in your python must be on the same directory as the script from which you are importing. Is that so in your case?WHAT ERROR(s) DO YOU GET?

A good way to do this is to have a folder for your python modules and to set the environment variable PYTHONPATH to contain this folder. If you're on windows, search for environment variables in windows help and set the correct value. In this folder, you can create files like myModule.py and then use import myModule in all your python programs. You can also define subfolders like anotherModule which must contain a (possibly empty) file __init__.py and other python files like myProgram.py . You can then use import statements like import anotherModule.myProgram . Also, start reading this http://www.python.org/doc/2.5.2/tut/node8.html in the python tutorial.

An even easier workaround for being able to import modules from ANY directory is to modify the sys.path variable at run time. So at the beginning of your code do something like this:

import os,sys
sys.path.append('/path/to_my/module')
import my_custom_module

And if you're working with a hard-coded windows path make sure you escape your escape characters (ie, C:\\Documents and Settings\\Administrator\\My Documents )

A better way to handle paths is using os.path.join()

>>> import os
>>> os.path.join('C:\\', 'Documents and Settings', 'Administrator', 'My Documents')
'C:\\Documents and Settings\\Administrator\\My Documents'
>>>

On Linux, you can add a path to the PYTHONPATH variable in ~/.bashrc. Add this line
export PYTHONPATH=${PYTHONPATH}:/new/path:/another/path:/colon/separates
The next time you boot, the PYTHONPATH will reflect the changes.

I would go with jlm699 ...

sys.path lists all the directories Python will check, so simply append it with the path of a directory you have created for all your modules.

This question has already been answered. Start a new discussion instead.