1. 程式人生 > 實用技巧 >windows在WSL+CONDA環境自啟動

windows在WSL+CONDA環境自啟動

自己做了個python應用,因為依賴相對複雜,所以使用WSL(-ubuntu)的方式部署。

在配置服務隨Windows系統自動化啟動的時候遇到問題。

因為需要切換環境,所以在使用以下語句,執行啟動指令碼時:

wsl -d ubuntu-18.04 -u chen "/etc/init.d/start-script"

最直接的想法,使用start-script指令碼內容如下:

#! /bin/bash
conda activate env_name
cd /mnt/e/path/to/script
python test.py

但是,在使用conda activate命令切換環境,會因為conda環境沒有初始化的問題,無法進入相應環境。

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'.

這時候使用下面語句切換環境即可:

source /home/user_name/miniconda3/bin/activate env_name
# 或者
source /home/user_name/anaconda3/bin/activate env_name

修改後的啟動指令碼為:

#! /bin/bash
source /home/user_name/miniconda3/bin/activate env_name
cd /mnt/e/path/to/script
python test.py

參考

calling-conda-source-activate-from-bash-script