【问题标题】:python on linux: what are all the possible place where PYTHONPATH can be set?linux上的python:可以设置PYTHONPATH的所有可能位置是什么?
【发布时间】:2012-04-26 11:50:09
【问题描述】:

我想清理我的 PYTHONPATH。

我知道我将它附加在 /home/me/.profile 中,如下所示:

PYTHONPATH=/home/hoff/code/someproject/pythonmods:$PYTHONPATH
PYTHONPATH=/home/hoff/code/google_appengine/google/appengine/tools:$PYTHONPATH
export PYTHONPATH

但是我必须在其他地方附加了 PYTHONPATH,当我进入 python 解释器并查看 sys.path 时,有各种各样的附加目录。

我可以在哪里指定它们,即可以附加 PYTHONPATH(在 linux/ubuntu 上)的所有可能位置是什么?

【问题讨论】:

    标签: python ubuntu pythonpath


    【解决方案1】:

    sys.pathPYTHONPATH 不是一回事;前者包含后者。要找出PYTHONPATH 的值,请在shell 中执行echo $PYTHONPATH

    文件/usr/{local/,}lib/pythonX.Y/dist-packages/site.py 将更新sys.path,因此您可以导入使用apt-geteasy_installpip 安装的软件包。卸载这些包会减少你的sys.path(虽然不是你的PYTHONPATH)。

    【讨论】:

      【解决方案2】:

      如前所述...它几乎可以设置在任何地方。我会看的几个地方(假设您的登录 shell 是典型的 bash...)

      .bashrc
      /etc/profile
      Anything in /etc/profile.d/  (typically loaded from /etc/profile)
      ~/.bash_profile
      ~/.bash_login
      

      来自man bash

        When bash is invoked as an interactive login shell, or as
        a non-interactive shell with the --login option,  it  first
        reads  and  executes commands from the file /etc/profile, 
        if that file exists.  After reading that file, it looks for
        ~/.bash_profile, ~/.bash_login, and ~/.profile
      

         When an interactive shell that is not a login shell is started,
         bash reads and executes commands from  ~/.bashrc,  if that
         file exists.
      

      【讨论】:

      • .bashrc 是我要找的地方,里面有一堆“export pythonpath”...
      【解决方案3】:

      可以设置环境变量的任何地方。

      进程继承其父级的环境,因此您可以在任何阶段对其进行修改,无论是 shell 配置文件、shell 命令行中、您调用的任何中间脚本、python 脚本(带有 sys.path)或它使用的任何库.

      【讨论】:

        猜你喜欢
        • 2013-10-12
        • 1970-01-01
        • 1970-01-01
        • 2018-03-08
        • 1970-01-01
        • 2019-08-28
        • 1970-01-01
        • 2010-10-17
        • 1970-01-01
        相关资源
        最近更新 更多