use*_*883 9 python bash conda miniconda google-colaboratory
这个问题可能与 2018 年提出的问题重复: conda environment in google colab [google-colaboratory]
但答案对我不起作用。
我注意到在 2020 年 1 月的另一个问题 Google 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
Run Code Online (Sandbox Code Playgroud)
请注意警告:
...
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
Run Code Online (Sandbox Code Playgroud)
...
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
Run Code Online (Sandbox Code Playgroud)
请注意注释已生效:
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. <==
Run Code Online (Sandbox Code Playgroud)
%%bash
conda update conda -y -q
source /usr/local/etc/profile.d/conda.sh
conda init (or conda init bash if linux)
Run Code Online (Sandbox Code Playgroud)
或者
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. <==
Run Code Online (Sandbox Code Playgroud)
安装环境:就我而言:
!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'.
Run Code Online (Sandbox Code Playgroud)
%%bash
exec bash
Run Code Online (Sandbox Code Playgroud)
和
!source ~/.bashrc
Run Code Online (Sandbox Code Playgroud)
=> 没有效果
另请注意以下事项:
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'.
Run Code Online (Sandbox Code Playgroud)
现在我认为环境已激活,但事实并非如此,因为python仍然没有指向conda env:
!which python
#/usr/local/bin/python
Run Code Online (Sandbox Code Playgroud)
这是一个不同的结果,我尊重答案: 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)
Run Code Online (Sandbox Code Playgroud)
输出:
Python version
3.7.5 (default, Oct 25 2019, 15:51:11)
[GCC 7.3.0]
Run Code Online (Sandbox Code Playgroud)
另请注意,不source推荐使用以下方式激活:
%%bash
source deactivate
#DeprecationWarning: 'source deactivate' is deprecated. Use 'conda deactivate'.
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏。
对于 Google 人员来说,默认情况下在 Colab 中设置 conda(或选择列表中的选项)将非常感激。
rch*_*urt 11
这对我有用,但如果您在尝试后仍然遇到问题,请告诉我:
%%shell
eval "$(conda shell.bash hook)" # copy conda command to shell
conda activate myenv
python --version
conda deactivate
Run Code Online (Sandbox Code Playgroud)