【发布时间】:2020-10-17 23:21:22
【问题描述】:
此问题可能与 2018 年提出的问题重复: conda environment in google colab [google-colaboratory]
但答案对我不起作用。
我注意到在 2020 年 1 月的另一个问题中也提到了它, osmNX in Google Colab
并注意到数据科学堆栈上发布了相同的问题 - 但答案对我也不起作用: https://datascience.stackexchange.com/questions/75948/how-to-setup-and-run-conda-on-google-colab/75979#75979
所以我认为问题仍然有效。
如何在 Colab 中激活 conda 环境?
找不到锻炼的方法。
重现步骤:
- 安装 miniconda
!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 激活已被弃用:
%%bash
source deactivate
#DeprecationWarning: 'source deactivate' is deprecated. Use 'conda deactivate'.
任何帮助表示赞赏。
附:对于 Google 人员 - 默认情况下在 Colab 中设置 conda(或从选定列表中选择)将不胜感激。
【问题讨论】:
标签: python bash conda google-colaboratory miniconda