首页 技术 正文
技术 2022年11月16日
0 收藏 323 点赞 4,723 浏览 3732 个字

一、Mac下,可能存在的 python 环境:
1、Mac系统自带的python环境在(由于不同的 mac 系统,默认自带的 python 版本可能不一样):
Python 2.7.10: /System/Library/Frameworks/Python.framework/Versions/2.7
其中,解释器在该目录下的 ./bin/python2.7
Python 2.6.9: /System/Library/Frameworks/Python.framework/Versions/2.6
其中,解释器在该目录下的 ./bin/python2.6
2、用户安装的python环境默认环境在:
Python 3.4.2: /Library/Frameworks/Python.framework/Versions/3.4
其中,解释器在该目录下的 ./bin/python3.4
Python 2.7.7: /Library/Frameworks/Python.framework/Versions/2.7
其中,解释器在该目录下的 ./bin/python2.7
注意:一般用户再装一遍python环境的时候,在终端的输入python命令,默认启动的python命令是当前用户安装的python版本环境
(即:系统默认的py启动路径,由原来的mac系统默认的解释器变为当前用户安装的py解释器 —— 参考本文最下面的可执行命令的寻找顺序问题)
3、 用户安装Anaconda3后,自带的python环境在:
Python 3.4.1: /Users/steven/Anaconda3
其中,解释器在该目录下的 ./bin/python3.4
(Anaconda3这个文件夹名,是在anaconda在安装时候的需要自定义的)
4、用户安装Anaconda后,自带的python环境在:
Python 2.7.8: /Users/steven/anaconda
其中,解释器在该目录下的 ./bin/python2.7

对于以上各环境,要想知道当前环境下的包路径,可通过一下命令查询:
import sys
print(‘\n’.join(sys.path))
该方法在排查包的引用问题的时候,特别有用!

二、关于以上的不同 python 环境的启动命令存储路径,以及启动(查找)顺序:
以上介绍了不同python环境所存放的目录,那在使用过程中,到底启动的是哪个环境呢?
首先:
1、Mac系统自带的python环境默认启动路径在:/usr/bin
2、用户安装的python环境默认启动路径在:/usr/local/bin
3、第三方的 python 环境,比如 anaconda 中的 python 环境,默认启动配置在文件中,通常为:~/.bash_profile
(.bash_profile 是在bash(默认的 shell 环境下)的配置文件,像我的电脑现在是 zsh 的 shell 环境,配置文件为.zshrc)
重点来了:
输入 python 命令是:会按照321的顺序查找含有 python 可执行文件,并执行。
该顺序同样适用于,mac 下所有的可执行命令的查找顺序!!!

三、怎么把自己安装的 python 版本设置为默认启动版本:
通过二,我们知道 python 在多版本的环境下的查找顺序:Mac在启动后,会先加载系统配置文件(包括~/.bash_profile )中的路径;并且,在同一配置文件中,可执行命令的路径是从后向前查找,后面路径覆盖前面路径。例如,在终端输入“python”,系统会在配置文件中的路径中从后向前查找,一直到找到为止。
例如:我当前.bash_profile的内容为:
# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH=”/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}”
export PATH

# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
PATH=”/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}”
export PATH

# added by Steven Liu
export PATH=”/Users/steven/Anaconda3/bin:$PATH”

# added by Anaconda 2.1.0 installer
export PATH=”/Users/steven/anaconda/bin:$PATH”

# added by Anaconda3 2.1.0 installer
export PATH=”/Users/steven/Anaconda3/bin:$PATH”

# Setting PATH for Subversion 1.9.0
export PATH=”/opt/subversion/bin:$PATH”
根据该脚本,先会去找 /opt/subversion/bin 中,有没有命名为“python”的可执行文件(或是快捷方式),发现没有,则往上找,接着找 /Users/steven/Anaconda3/bin ,发现有,就为当前路径下的解释器环境,并执行。
—— 所以,想设置python的版本,直接把你想添加的路径export上去,并放在后面。实测有效!
但是有个细节需要特别注意:
在添加了所需的路径在配置文件的最后之后,如果要想立即看到效果,需要两个步骤:
1.执行 soure .bash_profile;
2.新建 command 窗口查看效果(之前已经打开的窗口是看不到效果的)

四、关于多个 python 环境的卸载问题:
有时候安装的python版本太多,尤其 python 又分2和3,难免安装的东西会乱窜,为避免这种情况,可以试着删除自己安装的某些环境。
删除环境分几步:
1、删除Python框架
sudo rm -rf /Library/Frameworks/Python.framework/
如果有多个版本,而只需要删除一个版本,则:sudo rm -rf /Library/Frameworks/Python.framework/Versions/x.x
2、删除Python程序
sudo rm -rf “/Applications/Python x.x”
3、删除/usr/local/bin目录下的Python连接
brew prune
该命令的输出通常形为:Pruned 48 symbolic links and 18 directories from /usr/local
4、可选步骤:通过brew(Homebrew,是Mac OSX上的软件包管理工具)重新链接新的 python 环境:
brew doctor ——> 通过该命令诊断系统中当前的环境配置情况
sudo brew link python3 ——> 链接到新的 python3环境
其他可参考:http://stackoverflow.com/questions/22774529/what-is-the-safest-way-to-removing-python-framework-files-that-are-located-in-di

五、在执行可执行命令时,有一个小问题需要特别注意:当该命令在多个目录下存在时,到底调用的是什么路径下的命令?
以 python 命令为例:
如果输入带有“./”,则进入的当前目录的python环境
StevenLiu-MacBookPro:bin steven$ pwd
/Users/steven/Anaconda3/bin # 显示当前目录在python3的环境变量下
StevenLiu-MacBookPro:bin steven$ ./python # 根据当前路径找解释器
Python 3.4.1 |Anaconda 2.1.0 (x86_64)| (default, Sep 10 2014, 17:24:09)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> quit()
如果输入不带有“./”,则进入的是系统当前默认的python环境
StevenLiu-MacBookPro:bin steven$ python
Python 2.7.8 |Anaconda 2.1.0 (x86_64)| (default, Aug 21 2014, 15:21:46)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
Anaconda is brought to you by Continuum Analytics.
# 这里由于设置了anaconda中的python解释器为默认的python,所以不是
/Library/Frameworks/Python.framework/Versions/2.7

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848