16

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?

Ryan Saxe
  • 17,123
  • 23
  • 80
  • 128
  • `os.system("export PYTHONPATH=%s"%(":".join(sys.path),))` would probably work ... although Im not sure what you mean by permanent ... export usually only works until reboot ... normally you have an .bashrc file that does this kind of stuff every time you start up – Joran Beasley Oct 08 '13 at 19:18
  • Simply delete the directories. – Games Brainiac Oct 08 '13 at 20:19

5 Answers5

21

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.

Zack Su
  • 311
  • 2
  • 3
11

First, from terminal grab everything in your path by using

env | grep PYTHONPATH

Then, export your path and manually remove anything you no longer need:

export PYTHONPATH=[this is where you paste the corrected paths, no square brackets needed]

If you restart your session and you haven't modified anything in .bashrc, you can simply close and reopen your session.

mgoldwasser
  • 14,558
  • 15
  • 79
  • 103
  • This is the proper way of doing it given that you always want to run your python projects on a virtualenv, and you should not be modifying your system `.bashrc` file. – Azim Nov 28 '18 at 00:36
6

If the line you mention is in your .bashrc, it should be safe to simply delete it.

Exactly as it stands, what the line says is "add /my/path to PYTHONPATH", so it should be fairly safe even if there are others around your .bashrc.

Alois Mahdal
  • 10,763
  • 7
  • 51
  • 69
3

Your persistent Python path is usually set via a shell startup file like ~/.bashrc.

Modifying the PYTHONPATH variable within a shell will only change its value for the current instance of your shell and its childen when using 'export' but is by no mean intended to change its value permanently.

Use the following command to find where to modify your path:

grep -l PYTHONPATH ~/.*

If it's hardcoded in a startup file edit its value there, spawn a new shell and voila!

Alternatively a path may be added to Python's path via a .pth file in its existing path that refers to another location.

Should this be the case erasing it permanently from Python's path should be as simple as erasing this file.

remote
  • 81
  • 1
  • 6
1

This is for windows users. so if you have any custom module installed using pip install --user -e <package>.

Then the module path can be found in .pth file. Generally it is named as easy-install.pth and can be found in site-packages directory. Try removing the entries from this file and then check the sys.path again.

Chandan Kumar
  • 1,066
  • 10
  • 16
  • This was also the case for MacOS -- my sys.path was being modified by `/usr/local/lib/python3.9/site-packages/easy-install.pth`. Thanks for posting this comment, I'd have never found it otherwise! – Michael Brundage Jun 18 '21 at 22:35