【问题标题】:Python: deletion of item in sys.path is undone each time I restart the interpreterPython:每次我重新启动解释器时都会撤消 sys.path 中的项目删除
【发布时间】:2012-07-24 22:13:20
【问题描述】:
>>> sys.path[6]
/path/to/django
>>> sys.path.pop(6)
/path/to/django

然后CTRL + D

$ python

>>> sys.path[6]
/path/to/django

亲爱的……

【问题讨论】:

  • 任何其他变量的突变/绑定也是如此。为什么这令人惊讶?
  • 那么如何从列表中永久删除此项目?
  • 每次启动python时删除它..
  • 我假设 sys 模块确保对该列表的更改也在 PYTHONPATH 中进行了调整。
  • @Bentley4 +1,我也是。在另一篇 Stack Overflow 文章中,一位名叫 Sergey 的用户谈到了这个主题 the change to sys.path is not permanent and only affects the current process. If you want to make a change system-wide you'll need to have a look inside the site-packages directory and do some changes. See the lnk to the site module documentation I linked above

标签: python path python-2.7 pythonpath


【解决方案1】:

sys.path 由外部维护的 PYTHONPATH 变量填充。更改此变量以在重新启动 python 时获得永久更改。

在 unix(包括 Mac)上,当使用 bash 时,它的配置很可能是这样的:

导出 PYTHONPATH=....

而且这个语句很可能隐藏在你的 .profile 或 .bashrc 文件中。

要在 Windows 中执行此操作,您需要执行类似的操作

SET PYTHONPATH=....

它可能在系统控制面板中的某个地方 - 在环境下。

如果您想查看 PYTHONPATH 的当前设置,请转到命令(终端)窗口并输入 echo $PYTHONPATH (unix) 或 echo %PYTHONPATH% (windows)

【讨论】:

  • 我想删除的特定 PYTHONPATH 不在 .bashrc 中,也不在 .profile 中。 pythonpath可能隐藏的任何其他文件?
  • /etc/profile,任何相关的启动脚本...您正在运行什么操作系统?
  • Linux(Ubuntu)。它不在/etc/profile.d中(没有/etc/profile)。
  • 我不小心做了$ export PYTHONPATH=/path/to/django:$PYTHONPATH,而不是插入我到 django 的真实路径。这将存储在哪里?
  • 只有在您的终端窗口...开始新的终端窗口应该清除它时,它才会持续。
猜你喜欢
  • 1970-01-01
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
  • 2022-11-17
  • 1970-01-01
  • 2021-01-24
  • 1970-01-01
  • 2011-12-13
相关资源
最近更新 更多