【问题标题】:PYTHONPATH error when trying to activate a virtual environment尝试激活虚拟环境时出现 PYTHONPATH 错误
【发布时间】:2016-01-24 20:48:36
【问题描述】:

更新:我现在将我的.bash_profile 重置为此(基于this blog post):

PYTHONPATH="${PYTHONPATH}:/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/:$PATH"
export PYTHONPATH

之后,我再次尝试 source activate 并收到此错误:

Failed to import the site module
Your PYTHONPATH points to a site-packages dir for Python 2.x but you are running Python 3.x!
     PYTHONPATH is currently: ":/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/:/Users/name/anaconda/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/Users/name/anaconda/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/name/bin:/usr/local/git/bin:/Library/TeX/texbin"
     You should `unset PYTHONPATH` to fix this.

这有一个主要问题,因为这不是我在.bash_profile 中设置的 PYTHONPATH... 现在,我继续执行PYTHONPATH=/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/

但是,即使我指向 3.4 目录,我仍然会收到此错误:

Your PYTHONPATH points to a site-packages dir for Python 2.x but you are running Python 3.x!
     PYTHONPATH is currently: "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/"
     You should `unset PYTHONPATH` to fix this.

使用unset PYTHONPATH 使我处于下面提到的相同位置...


我使用的是 macbook pro,通常使用 Python 3.X 进行操作。有时我需要使用 Python 2.7,并且我创建了一个 anaconda 虚拟环境以方便我这样做。从昨天开始,当我尝试在命令行中 source activate 我的 Python 2 虚拟环境时,我已经开始收到以下错误的 5 个实例:

Error in sitecustomize; set PYTHONVERBOSE for traceback:
KeyError: 'PYTHONPATH'

我再次执行了与 PYTHONVERBOSE=1 source activate python2 相同的命令并找到了 Traceback(每个错误实例都相同):

Traceback (most recent call last):
  File "/Users/name/anaconda/lib/python3.4/site.py", line 508, in execsitecustomize
    import sitecustomize
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/usr/local/lib/python2.7/site-packages/sitecustomize.py", line 15, in <module>
    str(sys.version_info[0]) + '.x!\n     PYTHONPATH is currently: "' + str(os.environ['PYTHONPATH']) + '"\n' +
  File "/Users/name/anaconda/lib/python3.4/os.py", line 635, in __getitem__
    raise KeyError(key) from None
KeyError: 'PYTHONPATH'

有谁知道我该如何解决这个问题?在完全知道自己在做什么之前,我不想调整任何文件。

【问题讨论】:

  • 你为什么要把$PATH粘贴到PYTHONPATH的值中?
  • 虚拟环境是怎么安装的。你在安装时使用了python2还是python3
  • @EtanReisner 这是我从不同路径复制粘贴的错误。接得好。如果我删除它仍然没有帮助......
  • @Transformer 我用过 Anaconda。
  • 嗯,我给了你python的答案

标签: python bash shell


【解决方案1】:

看起来您的 PYTHONPATH 似乎不再在您的系统上设置,并且代码假设环境中有 -something-。

我相信您可以使用以下命令在 mac 中设置空白路径;请记住,如果您的 python3.4 依赖于系统路径,这可能很危险!警告!:我不在 Mac 上,所以您可能需要在此处的右侧提供一些字符串,否则可能无法保存密钥

export PYTHONPATH=

如果您不想在系统范围内设置任何内容,您可以在 sitecustomize 运行之前简单地将变量添加到正在运行的 python 脚本中。 Buildout 允许您使用预脚本执行此操作(通过 zc.buildout 或 zc.recipes,我确信 Anaconda 有类似的东西),这将允许 PYTHONPATH 对于 3.4 和 2.7 有所不同,这就是您的首先尝试做:

import os
pypath = os.environ.get("PYTHONPATH", "")
os.environ["PYTHONPATH"] = pypath

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多