如何在 mac 上为 2.7 设置 python 路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45649473/
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
How to setup python path for 2.7 on mac?
提问by Diesel Kumar
I am aware that, this question has been asked and answered many times. But, still i couldn't get rid of this. I found out the following info on my mac.
我知道,这个问题已被多次询问和回答。但是,我仍然无法摆脱这一点。我在我的 mac 上发现了以下信息。
cd /Library/Frameworks/Python.framework/Versions/
Current -> 2.7
3.6
whereispython
/usr/bin/python
which python
/usr/bin/python
which -a python
/usr/bin/python
/usr/bin/python
python
Python 2.7.10 (default)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Even though, when i edit ~/.bash_profile
as below
即使,当我编辑~/.bash_profile
如下
# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
#PYTHONPATH
PYTHONPATH="${PYTHONPATH}:/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python"
export PYTHONPATH
Then, source ~/.bash-profile
. After restarting my mac & terminal, could see still /usr/bin/python
instead of pointing to /Library/../Python2.7
when executed which python
.
然后,source ~/.bash-profile
。重新启动我的 mac 和终端后,可以看到 still/usr/bin/python
而不是/Library/../Python2.7
在执行时指向which python
。
I uninstalled python and re-installed "pip install python"
And also, i could see python installed under /System/Library/Frameworks/Python.framework/Versions
我卸载了python并重新安装"pip install python"
而且,我可以看到python安装在/System/Library/Frameworks/Python.framework/Versions
2.6
2.7
2.5 -> 2.6
2.3 -> 2.6
Current -> 2.7
I am not sure what could be missing here. How can i set/add the python path in mac ? Please help me to understand and overcome this. Can you please help me here ?
我不确定这里可能缺少什么。如何在 mac 中设置/添加 python 路径?请帮助我理解并克服这一点。你能帮我吗?
Reference Adding python path to mac osx
回答by AK47
If you have multiple versions of Python installed on your computer there should be multiple binaries in /usr/bin for each one
如果您的计算机上安装了多个版本的 Python,则 /usr/bin 中的每个二进制文件都应该有多个
MacBook-Pro:~ ak47$ ls /usr/bin | grep python
> python
> python2.6
> python2.7
You can access each different interpreter by changing the trigger
您可以通过更改触发器来访问每个不同的解释器
"Explicit is better than implicit"
“显式胜于隐式”
MacBook-Pro:~ ak47$ python --version
> Python 2.7.10
MacBook-Pro:~ ak47$ python2.6 --version
> Python 2.6.9
MacBook-Pro:~ ak47$ python2.7 --version
> Python 2.7.10
MacBook-Pro:~ ak47$ python3 --version
> Python 3.5.2
回答by bsd007
Install via brew. brew install python2 or python3
.
brew sets the python path in mac automatically.
通过 brew 安装。brew install python2 or python3
. brew 会自动在 mac 中设置 python 路径。
回答by Achilleus
Indeed, as mentioned, installing an up-to-date version of python using Brew is probably the best way to proceed, but be aware that starting with python 2.7.13 Homebrew doesn't add that version of python to your path automatically. It warns you that you must add "export PATH="/usr/local/opt/python/libexec/bin:$PATH"" to your .bash_profile. Do that and everything will work as expected.
事实上,如前所述,使用 Brew 安装最新版本的 python 可能是最好的方法,但请注意,从 python 2.7.13 Homebrew 开始,不会自动将该版本的 python 添加到您的路径中。它警告您必须在 .bash_profile 中添加“export PATH="/usr/local/opt/python/libexec/bin:$PATH"”。这样做,一切都会按预期进行。
回答by Chaine
From the current directory you want to add in your python path
从要添加到 python 路径的当前目录中
export PYTHONPATH=$PYTHONPATH:`pwd`