0

I want to run the following bash script from inside a python script.

condo activate bne
CUDA_VISIBLE_DEVICES=1 python bne.py --model models/BNE_SGsc --fe ./embeddings/BioEmb/Emb_SGsc.txt --fi names.txt --fo output_BNE_SGsc.txt

My python script/function to do this is as follows -

def do_tensorflow_routine(path_name_file):
    os.chdir(os.path.join("../","bne_resources/"))
    os.system("conda activate bne")
    bne_command = "python bne.py --model models/BNE_SGsc --fe ./embeddings/BioEmb/Emb_SGsc.txt --fi names.txt --fo names_bne_SGsc.txt"
    subprocess.run(bne_command,shell=True)
    pass

However, I am getting the following error -

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

I am using a cluster and my system specifications are as follows -

   Static hostname: lab1
         Icon name: computer-server
           Chassis: server
        Machine ID: 104265a0ea5b48c1a3c5a9802294af66
           Boot ID: 4277780744ae448292d66a9ff39c76e2
  Operating System: Ubuntu 20.04.1 LTS
            Kernel: Linux 5.8.0-36-generic
      Architecture: x86-64

Please let me know if I am missing something. My basic question is how to run another python script from a given python script, especially when we need to activate and deactivate virtual environments, since the parent python script and child python script require 2 different virtual environments due to some dependency conflicts. Kindly help me. Thanks in advance, Megh

Megh
  • 67
  • 1
  • 1
  • 7
  • `os.system("conda activate bne")` only works within the shell that `os.system()` starts, which exits as soon as that `os.system()` finishes running. That doesn't mean that `conda activate` doesn't run, but it _does_ mean that each `os.system()` call is independent -- you can't expect one such call to change the state of the shell started by future ones. – Charles Duffy Aug 18 '21 at 19:57
  • You need _both_ the `conda activate` _and_ the code that depends on it to be run from the _same_ `subprocess.Popen` instance. – Charles Duffy Aug 18 '21 at 19:58
  • Hi @CharlesDuffy - thanks for your reply. I saw the related post that you mentioned which is similar to this, and I tried modifying the statement to the following - ` subprocess.run(f"""conda init bash conda activate bne python bne.py --model models/BNE_SGsc --fe ./embeddings/BioEmb/Emb_SGsc.txt --fi names.txt --fo names_bne_SGsc.txt conda deactivate bne""",shell=True, executable='/bin/bash', check=True)` But, I keep getting the same `CommandNotFoundError` as mentioned in the original post. Please let me know if I am missing something. Thank you again! – Megh Aug 19 '21 at 08:12
  • Could you edit the question to show the attempt (as informed by the existing question's answers) and the new error? I suspect some formatting got lost in putting it into a comment, and the details are critical. – Charles Duffy Aug 19 '21 at 16:15
  • Part of the likely problem is that `conda init` modifies dotfiles, but a noninteractive shell doesn't _read_ dotfiles and won't get any of the benefit of those modifications. – Charles Duffy Aug 19 '21 at 16:16
  • ...so, what you might need to do is `source` in whatever configuration conda relies on. See https://stackoverflow.com/a/52813960/14122 for an example of that. (I've added the question that answer is attached to to the duplicate list). – Charles Duffy Aug 19 '21 at 16:18

1 Answers1

-1

If the terminal does not show (base) after running conda activate bne, then try running:

conda init

In the bash script at the top you need the shebang line #!/usr/bin/env bash and env is used for portability for different distributions of Linux.

Shaqil Ismail
  • 1,794
  • 1
  • 4
  • 5
  • The OP isn't running `conda activate` in a terminal at all; they're running it in a noninteractive shell started by their Python process. Advice to look at what the terminal prompt shows is completely moot / irrelevant. Beyond that, this question has already been asked and answered elsewhere on the site already; in [How to Answer](https://stackoverflow.com/help/how-to-answer), note the section _Answer Well-Asked Questions_, and the bullet point therein regarding questions that "have already been asked and answered many times before". – Charles Duffy Aug 18 '21 at 19:59