Skip to content Skip to sidebar Skip to footer

How To Refresh Sys.path?

I've installed some packages during the execution of my script as a user. Those packages were the first user packages, so python didn't add ~/.local/lib/python2.7/site-packages to

Solution 1:

As explained in What sets up sys.path with Python, and when?sys.path is populated with the help of builtin site.py module.

So you just need to reload it. You cannot it in one step because you don't have site in your namespace. To sum up:

import site
from importlib import reload
reload(site)

That's it.

Solution 2:

It might be better to add it directly to your sys.path with:

import sys
sys.path.append("/your/new/path")

Or, if it needs to be found first:

import sys
sys.path.insert(1, "/your/new/path")

Post a Comment for "How To Refresh Sys.path?"