从python路径中永久删除目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19256359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
permanently remove directory from python path
提问by Ryan Saxe
I recently added some things to my python path that I don't want there using:
我最近在我的 python 路径中添加了一些我不想使用的东西:
export PYTHONPATH=$PYTHONPATH:/my/path
You can use sys.path.remove
to remove something in the path, but it's not 100% permanentlike the way I added it with the command line statement above.
您可以使用sys.path.remove
删除路径中的某些内容,但它不像我使用上面的命令行语句添加它的方式那样 100% 永久。
what can I do to permanently removedirectories from the python path?
我该怎么做才能从 python 路径中永久删除目录?
采纳答案by Alois Mahdal
If the line you mention is in your .bashrc, it should be safe to simply delete it.
如果您提到的行在您的 .bashrc 中,那么简单地删除它应该是安全的。
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.
就目前而言,该行所说的是“将 /my/path 添加到 PYTHONPATH”,因此即使您的 .bashrc 周围还有其他人,它也应该是相当安全的。
回答by mgoldwasser
First, from terminal grab everything in your path by using
首先,从终端使用
env | grep PYTHONPATH
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]
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.
如果您重新启动会话并且没有修改 .bashrc 中的任何内容,则只需关闭并重新打开会话即可。
回答by remote
Your persistent Python path is usually set via a shell startup file like ~/.bashrc.
你的持久性 Python 路径通常是通过像 ~/.bashrc 这样的 shell 启动文件来设置的。
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.
在 shell 中修改 PYTHONPATH 变量只会在使用 'export' 时更改 shell 的当前实例及其子代的值,但绝不意味着永久更改其值。
Use the following command to find where to modify your path:
使用以下命令查找修改路径的位置:
grep -l PYTHONPATH ~/.*
grep -l PYTHONPATH ~/.*
If it's hardcoded in a startup file edit its value there, spawn a new shell and voila!
如果它被硬编码在启动文件中,请在那里编辑它的值,生成一个新的 shell,瞧!
Alternatively a path may be added to Python's path via a .pth file in its existing path that refers to another location.
或者,可以通过引用另一个位置的现有路径中的 .pth 文件将路径添加到 Python 的路径。
Should this be the case erasing it permanently from Python's path should be as simple as erasing this file.
如果是这种情况,从 Python 的路径中永久擦除它应该就像擦除这个文件一样简单。
回答by Zack Su
If you simply delete the line "export PYTHONPATH=..." in .bashrc and do "source .bashrc", those directories would still be in sys.path.
如果您只是删除 .bashrc 中的“export PYTHONPATH=...”行并执行“source .bashrc”,则这些目录仍将位于 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.
与 .bashrc 中的“export PATH”不同,当您将某些目录导出到 PYTHONPATH 时,它们似乎被转储到 Python 始终可以检查的某个文件中。
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.
因此,您需要做的是“导出 PYTHONPATH=”(导出空字符串)并执行“source .bashrc”。这将清理您在 .bashrc 中导出到 PYTHONPATH 之前的所有内容。
回答by Chandan Kumar
This is for windows users. so if you have any custom module installed using
pip install --user -e <package>
.
这是针对 Windows 用户的。因此,如果您使用
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.
然后可以在 .pth 文件中找到模块路径。通常它被命名为easy-install.pth
并且可以在 site-packages 目录中找到。尝试从此文件中删除条目,然后sys.path
再次检查。