I'm assuming you mean `sys.path`, not `os.path`. If you want to be able to find and import Python code, there are better ways to do it.
First thing, though. If the problem is that you have multiple subfolders of code, and you cannot seem to import it, you're missing a crucial component in your "package" structure. Do you have an `__init__.py` file in all of your source code directories? If not, you should have them--they can just be empty files. (See https://docs.python.org/2/tutorial/modules.html#packages.) With these, your plain folders become Python "packages". (A "module" is just a file containing Python code which has a .py extension. A "package" is a folder which contains an __init__.py "module".)
If you want to add the `foo` package to your path, so that you can import code from it, you can do this:
# you only need to add the root folder to your path
# if your folders have an __init__.py in them,
# python will traverse the folder structure and find all of the code
# now you can make all sorts of imports statements:
from foo import some_other_module
from foo.bar import baz
from foo.bar.blarg import utils
Instead of just putting code somewhere and patching the `sys.path` at runtime, there is a better way. It is better to install your code in a well-known way to a well-known location. See https://docs.python.org/2/distutils/setupscript.html. Another thing you can do add the path to the code to your PYTHONPATH environment variable. For distributing code, creating a proper `setup.py` file is better.
Edited by thok, 09 September 2014 - 03:18 AM.