简体   繁体   English

除了 OSX 上现有的 pyenv 安装外,如何安装 Anaconda?

[英]How can I install Anaconda aside an existing pyenv installation on OSX?

Sincerest apologies if this is easily found elsewhere, but although I found a number of posts with pyenv and Anaconda explanations, none addressed this issue specifically.如果这在其他地方很容易找到,最诚挚的歉意,但是虽然我发现了一些带有 pyenv 和 Anaconda 解释的帖子,但没有一个专门解决这个问题。 However, I am often an idiot.然而,我经常是个白痴。

On Mac OSX (Mojave 10.14.6) I installed pyenv via Homebrew在 Mac OSX (Mojave 10.14.6) 上,我通过 Homebrew 安装了 pyenv

brew install pyenv

And I happily install and switch between Python versions with我很高兴地安装并在 Python 版本之间切换

pyenv install...

and

pyenv global...

I typically use VS Code as my IDE.我通常使用 VS Code 作为我的 IDE。

I now have need to do some work in Anaconda.我现在需要在 Anaconda 中做一些工作。 I haven't used it before.我以前没用过。 Can I simply install Anaconda via the distribution site and use its navigator, and when I need my old python versions use pyenv and VS Code, or will there be a conflict when I install Anaconda?我可以通过分发站点简单地安装 Anaconda 并使用它的导航器,当我需要旧的 python 版本时使用 pyenv 和 VS Code,或者我安装 Z853F30A23CE8B8CCBD6946C91D675 时会发生冲突吗? If there would be a conflict, is there a path to running both on OSX?如果会有冲突,是否有在 OSX 上同时运行的路径?

I could install it and see what happens of course, and restore from backup if it's a big mess.我可以安装它,看看会发生什么,如果它很乱,可以从备份中恢复。 But I'm hoping that a pyenv / Anaconda guru might have some sage words of advice that would save me potentially hours of cleaning up.但我希望 pyenv / Anaconda 大师可能有一些明智的建议,可以节省我数小时的清理时间。

Thanks in advance!提前致谢!

Not super familiar with conda but I do use pyenv a lot. 对conda不太熟悉,但是我经常使用pyenv。

Pyenv has its own virtualenv manager that you can use. Pyenv有其自己的virtualenv管理器 ,您可以使用。 You can always check which virtualenv version is active with: 您始终可以使用以下命令检查哪个virtualenv版本处于活动状态:

pyenv versions

You should see something like: 您应该看到类似以下内容:

  system
  20190814_125309
* 3.7.4 (set by /home/tzhuang/.pyenv/version)
  3.7.4/envs/20190814_125309
  3.7.4/envs/buildmaster-sandbox
  3.7.4/envs/HEAD
  3.7.4/envs/myenv
  3.7.4/envs/sandbox
  buildmaster-sandbox
  HEAD
  myenv
  sandbox

Where the * indicates the currently active virtualenv (this can be set using pyenv global like you mentioned). *表示当前处于活动状态的virtualenv(可以使用您提到的pyenv global进行设置)。 You can manually activate any virtualenv with: 您可以使用以下方法手动激活任何virtualenv:

pyenv shell

Eg. 例如。

pyenv shell sandbox

Then running pyenv versions gives: 然后运行pyenv versions会给出:

  system
  20190814_125309
  3.7.4 (set by /home/tzhuang/.pyenv/version)
  3.7.4/envs/20190814_125309
  3.7.4/envs/buildmaster-sandbox
  3.7.4/envs/HEAD
  3.7.4/envs/myenv
  3.7.4/envs/sandbox
  buildmaster-sandbox
  HEAD
  myenv
* sandbox

It's generally a good idea to install any packages you want into a new virtualenv instead of the global virtualenv. 通常,将所需的任何软件包安装到新的virtualenv而不是全局virtualenv中都是一个好主意。 It makes it easier to debug environment/dependency issues should you run into any. 如果遇到任何环境/依赖关系问题,调试起来会更容易。

There is a conflict, cause both pyenv and conda try to expose a global Python environment by default. 有冲突,导致两个pyenvconda尝试在默认情况下,露出一个全球性的Python环境。

I've been using these tools together and best solution found by me is to 我一直在使用这些工具,而我发现的最佳解决方案是

  1. Alway initialize pyenv , use the Python set by pyenv global as the default Python pyenv初始化pyenv ,使用pyenv global设置的Python作为默认Python
  2. Only expose command conda but do NOT activate any environment from it 仅公开命令conda但不从中激活任何环境

Detail 详情

Since pyenv has been installed on your machine, you only need to install Anaconda. 由于pyenv已安装在您的计算机上,因此只需要安装Anaconda。

brew cask install anaconda

Init conda without exposing the "base" environment from conda . 初始化conda而不暴露“基地”环境conda

# init conda, the following command write scripts into your shell init file automatically
conda init

# disable init of env "base"
conda config --set auto_activate_base false

Done. 做完了

Note: After this setup, the default Python is the one set by pyenv global . 注意:此设置后,默认的Python是pyenv global设置的Python。 Use pyenv and conda to manage environments separately. 使用pyenvconda分开管理环境。

Examples of managing virtual environments. 管理虚拟环境的示例。

# virtual environments from pyenv
pyenv install 3.6.9
pyenv virtualenv 3.6.9 new-env
pyenv activate new-env
pyenv deactive
# You can also use `pyenv local`


# virtual environments from conda
conda create -n new-env python=3.6
conda env list
conda activate new-env
conda deactivate

Default env location for pyenv is ~/.pyenv/versions . pyenv默认环境位置是~/.pyenv/versions

Default env location for conda , check output from conda info . conda默认环境位置,请检查conda info输出。

Extended Readign 扩展准备

There is a much simpler solution to the one mentioned by Simba . Simba提到的解决方案有一个更简单的解决方案。

You can use pyenv-virtualenv您可以使用pyenv-virtualenv

It allows you to manage virtual environments (including conda ones) right from pyenv.它允许您直接从 pyenv 管理虚拟环境(包括 conda 环境)。

Here is an example scenario:这是一个示例场景:

  • I have python 3.10.5 installed with pyenv and set as the global Python version.我有 python 3.10.5 安装了 pyenv 并设置为全局 Python 版本。
  • I installed anaconda3-2022.05 with pyenv, and I want to use a conda environment in a project.我用pyenv安装了anaconda3-2022.05,我想在项目中使用conda环境。
  • I installed pyenv-virtualenv with Homebrew (since I installed pyenv with Homebrew as well) and I placed its init in my .zshrc .我用 Homebrew 安装了 pyenv-virtualenv(因为我也用 Homebrew 安装了 pyenv)并将它的 init 放在我的.zshrc中。
  • I created a virtual conda environment using pyenv: pyenv virtualenv anaconda3-2022.05 my-conda-env我使用 pyenv 创建了一个虚拟 conda 环境: pyenv virtualenv anaconda3-2022.05 my-conda-env
  • I navigated to the project folder and: pyenv local my-conda-env我导航到项目文件夹和: pyenv local my-conda-env
  • Now the .python-version file refers to my-conda-env and each time I navigate to that folder in the terminal my-conda-env will autoactivate, and will deactivate when I navigate out of the project folder (this is because we have the init of pyenv-virtualenv loaded).现在.python-version文件引用my-conda-env ,每次我导航到终端中的该文件夹时,my-conda-env 都会自动激活,当我导航出项目文件夹时会停用(这是因为我们有pyenv-virtualenv 的 init 已加载)。

Ps in my experience you don't need to place the line that auto disables anaconda base environment in your shell config file. Ps 根据我的经验,您不需要在 shell 配置文件中放置自动禁用 anaconda 基本环境的行。 The base environment didn't auto activate for me anyways.无论如何,基本环境并没有为我自动激活。

Note: also be mindful of the placements of the three additions to your shell config file: the pyenv one, the pyenv-virtualenv one, and the anaconda one.注意:还要注意 shell 配置文件中三个添加项的位置:pyenv 一个、pyenv-virtualenv 一个和 anaconda 一个。 The one placed later in the file is the one that loads last and hence its adjustment to the PATH will take the most precedent.放在文件后面的那个是最后加载的那个,因此它对 PATH 的调整将采取最先例。

For those on Windows , the Conda installer automatically handles this for you.对于 Windows 上的用户,Conda 安装程序会自动为您处理。 Just uncheck "Add Anaconda3 to my PATH environment variable," and uncheck "Register Anaconda3 as my default Python 3.9".只需取消选中“将 Anaconda3 添加到我的 PATH 环境变量”并取消选中“将 Anaconda3 注册为我的默认 Python 3.9”。

If you do that, you won't have any conflicts with pyenv.如果这样做,您将不会与 pyenv 发生任何冲突。 Just open the conda terminal when you want to use conda and continue using your normal terminal as before.当您想使用 conda 时,只需打开 conda 终端,然后像以前一样继续使用您的普通终端。

Explanation :说明

  1. Choose whether to add Anaconda to your PATH environment variable or register Anaconda as your default Python.选择是将 Anaconda 添加到 PATH 环境变量中,还是将 Anaconda 注册为默认 Python。 We don't recommend adding Anaconda to your PATH environment variable, since this can interfere with other software.我们不建议将 Anaconda 添加到您的 PATH 环境变量中,因为这会干扰其他软件。 Unless you plan on installing and running multiple versions of Anaconda or multiple versions of Python, accept the default and leave this box checked.除非您计划安装和运行 Anaconda 的多个版本或 Python 的多个版本,否则请接受默认值并选中此框。 Instead, use Anaconda software by opening Anaconda Navigator or the Anaconda Prompt from the Start Menu.相反,通过从开始菜单打开 Anaconda 导航器或 Anaconda 提示来使用 Anaconda 软件。 - Windows Install Instructions - Windows 安装说明

anaconda 设置截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM