permanently remove directory from python path

Ryan Saxe picture Ryan Saxe · Oct 8, 2013 · Viewed 29.7k times · Source

I recently added some things to my python path that I don't want there using:

export PYTHONPATH=$PYTHONPATH:/my/path

You can use sys.path.remove to remove something in the path, but it's not 100% permanent like the way I added it with the command line statement above.

what can I do to permanently remove directories from the python path?

Answer

Zack Su picture Zack Su · Aug 28, 2014

If you simply delete the line "export PYTHONPATH=..." in .bashrc and do "source .bashrc", those directories would still be in sys.path.

Unlike "export PATH" in .bashrc, it seems that when you export some directories into PYTHONPATH, they are dump into some file which python can always check.

So, what you need to do is "export PYTHONPATH=" (export empty string) and do "source .bashrc". This will clean up everything you have export into PYTHONPATH before in .bashrc.