changing sys.path

Rick Johnson rantingrickjohnson at
Wed Feb 1 18:17:04 CET 2012

On Feb 1, 10:15 am, Andrea Crotti <andrea.crott... at> wrote:
> So suppose I want to modify the sys.path on the fly before running some code
> which imports from one of the modules added.
> at run time I do
> sys.path.extend(paths_to_add)
> but it still doesn't work and I get an import error.
> If I take these paths and add them to site-packages/my_paths.pth
> everything works, but at run-time the paths which I actually see before
> importing are exactly the same.

1. Is paths_to_add a nested list?
2. Have you tried inspecting the contents of sys.path AFTER calling
extend method?

py> sys.path.__len__()
py> sys.path.extend([[1,2,3]])
py> sys.path.__len__()

More information about the Python-list mailing list