permanently remove directory from python path

2019-04-06 14:31发布

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?

4条回答
Melony?
2楼-- · 2019-04-06 14:57

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.

查看更多
放荡不羁爱自由
3楼-- · 2019-04-06 14:58

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.

查看更多
可以哭但决不认输i
4楼-- · 2019-04-06 14:59

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.

查看更多
何必那么认真
5楼-- · 2019-04-06 15:04

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.

查看更多
登录 后发表回答