这个问题可能与 2018 年提出的问题重复: google colab 中的 conda environment [google-colaboratory]
但答案对我不起作用。
我注意到它也在 2020 年 1 月的另一个问题中提到, 谷歌 Colab 中的 osmNX
并注意到数据科学堆栈上发布了相同的问题 - 但答案对我也不起作用: https ://datascience.stackexchange.com/questions/75948/how-to-setup-and-run-conda-on-google-colab/75979 #75979
所以我认为问题仍然有效。
如何在 Colab 中激活 conda 环境?
找不到锻炼的方法。
重现步骤:
- 安装迷你康达
!wget https://repo.continuum.io/miniconda/Miniconda3-4.5.4-Linux-x86_64.sh
!bash Miniconda3-4.5.4-Linux-x86_64.sh -bfp /usr/local
注意警告:
...
installation finished.
WARNING:
You currently have a PYTHONPATH environment variable set. This may cause
unexpected behavior when running the Python interpreter in Miniconda3.
For best results, please verify that your PYTHONPATH only points to
directories of packages that are compatible with the Python interpreter
in Miniconda3: /usr/local
- 更新和 bash 变量
%%bash
conda update conda -y -q
source /usr/local/etc/profile.d/conda.sh
conda init (or conda init bash if linux)
请注意注释以更改效果:
no change /usr/local/condabin/conda
no change /usr/local/bin/conda
no change /usr/local/bin/conda-env
no change /usr/local/bin/activate
no change /usr/local/bin/deactivate
no change /usr/local/etc/profile.d/conda.sh
no change /usr/local/etc/fish/conf.d/conda.fish
no change /usr/local/shell/condabin/Conda.psm1
no change /usr/local/shell/condabin/conda-hook.ps1
no change /usr/local/lib/python3.7/site-packages/xontrib/conda.xsh
no change /usr/local/etc/profile.d/conda.csh
modified /root/.bashrc
==> For changes to take effect, close and re-open your current shell. <==
- 尝试使更改生效:
%%bash
exec bash
或者
!source ~/.bashrc
安装环境:在我的情况下:
!conda env create -f enviroment.yml
激活环境 > 不起作用!
!conda activate myenv
注意评论:
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run
$ conda init <SHELL_NAME>
Currently supported shells are:
- bash
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
- 所以我再试一次:
!conda init bash
和
!conda activate myenv
=> 没有效果
另请注意以下事项:
%%bash
source activate myenv
现在我认为环境被激活了,但不是这样,因为python仍然没有指向conda env:
!which python
#/usr/local/bin/python
这是我对答案的不同结果: https ://datascience.stackexchange.com/a/75979
看到 python 指向默认值,而不是 conda 的:
%%bash
source activate myenv
python
import sys
# maybe only need this the first time we run this notebook
sys.path.append('/usr/local/lib/python3.6/site-packages')
print("Python version")
print(sys.version)
输出:
Python version
3.7.5 (default, Oct 25 2019, 15:51:11)
[GCC 7.3.0]
另请注意,source
不推荐使用 with 激活:
%%bash
source deactivate
#DeprecationWarning: 'source deactivate' is deprecated. Use 'conda deactivate'.
任何帮助表示赞赏。
谷歌人的 Ps - 默认情况下在 Colab 中设置 conda(或从选定列表中选择)将不胜感激。