系统环境
- MacBook Pro (Retina, 13-inch, Late 2013)
- macOS Catalina Version 10.15.4
查看 macOS 内的自带 Python,你会惊讶 Catalina 现在还自带了一个 Python 3,版本号还不低。这里有人讨论。
/usr/bin/python
% which python3
/usr/bin/python3
% python --version
Python 2.7.16
% python3 --version
Python 3.7.3
默认的还是过时的 Python 2。如果你刚开始学 Python,没关系,直接用 python3 启动解释器就可以学习了。不过,这个 Python 3 没有 IDLE。
如果你要在 macOS 上新建一个 Python 开发环境,怎么办?请仔细读一下这篇文章。
但是......这篇文章也没有解决一个问题,我现在要用 Anaconda,conda 和 pyenv 两者之间有冲突,这个怎么办?看看这个讨论。也看看这篇文章 Michael Sarahan 的建议,都是一个意思。
我是怎么做的呢?
先根据这篇文章的说明用 pyenv 安装了最高可安装版本 python 3.8.2,因此里面的 3.8.1 改成 3.8.2。最后一步需要 restart 你的 terminal 后 python -V 才能显示最新安装的版本。请也查查 pip 版本。
然后在 homebrew 下安装 Anaconda
~ brew cask install anaconda
安装完成后要对 Anaconda 进行初始化,初始化的理由看上面 Michael Sarahan 的说明。我在这里引述一下:
“Modifying PATH can cause problems if there are any other programs on your system that have the same names, that Anaconda then hides (shadows) by being found first. What “conda init” does is to set up a conda “shell function” and keep the other stuff off PATH. Nothing but conda is on PATH. It then defaults to activating your base environment at startup. The net effect is very much like your PATH addition, but has some subtle, but critically important differences:
activation ensures that anaconda’s PATH stuff is right up front. Putting anaconda at the front of PATH permanently is good in that it prevents confusion, but bad in that it shadows other stuff and can break things. Activation is a less permanent way to do this. You can turn off the automatic activation of the base environment using the “auto_activate_base” condarc setting.
activation does a bit more than just modifying PATH. It also sources any activate.d scripts, which may set additional environment variables. Some things, such as GDAL, require these. These packages will not work without activation.
So, rather than your tip on modifying PATH, what we recommend is following the directions at the end of the installer.
Should you choose not to run conda init:
…
that will look like 2 commands:
1. eval “$(/home/msarahan/mc3_dummy/bin/conda shell.bash hook)”
2. conda init
Note that in step 1, I changed the shell name from YOUR_SHELL_NAME to bash. You may need to adjust that yourself to your shell.”
如何自己手动初始化?在 Anaconda 的官方网站上现在有介绍,请看 Installing on macOS 的第 7 步。
If you enter “no”, then conda will not modify your shell scripts at all. In order to initialize after the installation process is done, first run source <path to conda>/bin/activate and then run conda init.
If you are on macOS Catalina, the new default shell is zsh. You will instead need to run source <path to conda>/bin/activate followed by conda init zsh.
我用的 zsh,所以先:
~ source /usr/local/anaconda3/bin/activate
(base) ~ conda init zsh
你会看到 prompt 多了一个 (base),表示目前在 Anaconda base env 下。
重新启动 terminal,然后再确认一下是否正常工作:
(base) ~ conda
(base) ~ ipython
(base) ~ jupyter notebook
(base) ~ python
你会看到都能正常运行,而且 python 现在是 Anaconda 带的版本,而不是之前安装安装的 3.8.2 版本。回到 3.8.2 版本,要退出 Anaconda base env,使用下面的命令:
(base) ~ conda deactivate
(base) 消失,再查看就是默认的 Python 版本了。
~ python -V
现在 homebrew 有一个 python 3.8.2,还有一个 Anaconda。两个互不干扰,你可以在各自的环境下建立 Python Virtual Environment。
初始化后,查看 .zshrc 文件看看发生了什么变化:
~ less .zshrc
有一大块 shell script:
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
...
# <<< conda initialize <<<
现在你每次启动 terminal 都会自带打开 Anaconda 的 base env。prompt 前面会出现一个 (base)。你可以修改 conda 的配置文件 .condarc 关闭自动启动。看官方说明如何修改。下面的命令即可:
(base) ~ conda config --set auto_activate_base False
(base) ~ conda deactivate
重启 terminal,再打开已经看不到 (base) 了。
再进入 Anaconda 下,输入以下命令:
~ conda activate
No comments:
Post a Comment