Search code examples
condapopeninitvirtual-environment

How to conda init in a shell which is executed by Popen?


So I am trying to use Popen in Qt to execute a shell in which I would like to activate a conda virtual environment called”alphapose” and then execute inference.py:

QString srtttt = "conda activate alphapose;cd;cd AlphaPose-pytorch-1.11 ;python scripts/demo_inference.py ";
FILE* fp = popen(srtttt.toLocal8Bit().data(), "r");

Yet it tells me that


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.

I know the next step would be conda -init and reopen the terminal, and here’s the question: how to reopen the shell executed by Popen"?If you don’t close, then conda will never init. If you close, then Popen is over so the execution is over.

Actually, I don’t think of anything to solve this. So the following is just some add-up to the situation: I can successfully run conda activate or other commands in the terminal, which means conda is fine. Just this shell executed by Popen can’t init well. In this particular shell,”conda env list” or “conda” can be executed well with the right output, if you execute “conda init”, the output will be “no change and no action taking”(which is weird)


Solution

  • In principle, it is the same answer as here, namely: use conda run - the conda activate command is only intended for interactive shell sessions. So, try something like:

    QString srtttt = "cd AlphaPose-pytorch-1.11;conda run -n alphapose python scripts/demo_inference.py";
    FILE* fp = popen(srtttt.toLocal8Bit().data(), "r");
    

    You may need a --live-stream flag if you need to stream the output.