解决外部程序激活conda环境

最终实现C++程序调用终端命令,运行sh脚本文件,通过sh文件激活虚拟环境。

参考:【Linux】用shell脚本激活conda虚拟环境_Keep Moving-CSDN博客

运行成功的sh文件内容(命名test.sh)

# added by Archiconda3 0.2.2 installer
# >>> conda init >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false '/home/xtark/archiconda3/bin/conda' shell.bash hook 2> /dev/null)"
if [ $? -eq 0 ]; then
    \eval "$__conda_setup"
else
    if [ -f "/home/xtark/archiconda3/etc/profile.d/conda.sh" ]; then
        . "/home/xtark/archiconda3/etc/profile.d/conda.sh"
        CONDA_CHANGEPS1=false conda activate base
else
        \export PATH="/home/xtark/archiconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda init <<<
conda activate f

 C++程序调用终端命令

system("source test.sh);

遇到的一些问题:

sh: 1: source: not found

参考:sh: 1: source: not found on ubuntu_Jack的博客-CSDN博客

原因说明:
1、Ubuntu系统中,默认使用的shell是/bin/dash,它不支持source命令;
2、由于大多数shell都是使用的/bin/bash,因此简单粗暴,直接改变shell运行环境

解决:

sudo dpkg-reconfigure dash #选择No

显示:/bin/sh -> dash

sudo dpkg-reconfigure dash #选择No
ls -l `which sh`

显示:/bin/sh -> bash

conda:command not found

这个问题是因为我们刚开始的sh脚本文件只写了conda activate f

没有写前面的一些,我的理解是没有在脚本文件配置环境变量也是不行的,和以下问题类似。

【Linux】conda: command not found解决办法_Vincent Lai的博客-CSDN博客

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Clark-dj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值