尝试激活虚拟环境时出现PYTHONPATH错误 [英] PYTHONPATH error when trying to activate a virtual environment

查看:58
本文介绍了尝试激活虚拟环境时出现PYTHONPATH错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我现在已将 .bash_profile 重置为此(基于

UPDATE: I've now reset my .bash_profile to this (based on this blog post):

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

之后,我尝试再次激活源并收到此错误:

After that, I tried source activate again and received this error:

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/版本/3.4/lib/python3.4/site-packages/

There is a major problem with this because that's not the PYTHONPATH set in my .bash_profile... Now, I proceeded to execute PYTHONPATH=/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/

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

However, I'm still getting this error even though I'm pointing to a 3.4 directory:

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.

使用未设置PYTHONPATH 会将我置于下面所述的相同位置...

Using unset PYTHONPATH puts me in the same position noted below...

我使用的是Macbook Pro,通常与Python 3.X一起运行.有时我需要使用Python 2.7,并且创建了anaconda虚拟环境以使其易于使用.从昨天开始,当我尝试在命令行中源代码激活我的Python 2虚拟环境时,我开始收到以下错误的5个实例:

I use a macbook pro and typically operate with Python 3.X. Sometimes I need to use Python 2.7 and I have created an anaconda virtual environment to make it easy for me to do so. Since yesterday, I've started receiving 5 instances of the below error when trying to source activate my Python 2 virtual env within the command line:

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

我再次执行了与 PYTHONVERBOSE = 1源激活python2 相同的命令,并找到了Traceback(对于每个错误实例都是相同的):

I executed the same command again as PYTHONVERBOSE=1 source activate python2 and located the Traceback (it's the same for each instance of the error):

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'

有人知道我该如何解决这个问题?在完全了解自己的工作之前,我不想调整任何文件.

Does anyone know how I may go about fixing this? I don't want to adjust any files before I fully know what I'm doing.

推荐答案

看来您的系统上不再设置了PYTHONPATH,并且代码假定环境中存在-something-.

it looks like your PYTHONPATH is no longer set on your system, and the code is assuming that there is -something- in the environment.

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

I believe you could set a blank path in mac using the following; keep in mind if your python3.4 relies on the system path this may be dangerous! WARNING!: I am not on a mac, so you might need to give some string on the right-hand side here, otherwise it might not save the key

export PYTHONPATH=

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

If you do not want to set anything system-wide, you could simply add the variable to the running python scripts before sitecustomize runs. Buildout allows you to do this with a pre-script (via zc.buildout or zc.recipes, I am sure Anaconda has something similar), and this would allow the PYTHONPATH to be different for 3.4 and 2.7, which is what you're trying to do in the first place:

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

这篇关于尝试激活虚拟环境时出现PYTHONPATH错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆