How to run a bash script from a python file? [duplicate] - python-3.x

This question already has answers here:
Activate conda environment using subprocess
(1 answer)
Conda command working in command prompt but not in bash script
(4 answers)
Closed 1 year ago.
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

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.

Related

Can't change virtual environment within Azure ML notebook

For some reason the jupyter notebooks on my VM are in the wrong environment (ie stuck in (base)). Furthermore, I can change the environment in the terminal but not in the notebook. Here is what happens when I attempt !conda activate desired_env in the notebook:
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'.
# conda environments:
#
base * /anaconda
azureml_py36 /anaconda/envs/azureml_py36
azureml_py38 /anaconda/envs/azureml_py38
azureml_py38_pytorch /anaconda/envs/azureml_py38_pytorch
azureml_py38_tensorflow /anaconda/envs/azureml_py38_tensorflow
I tried the answers here (e.g., first running !source /anaconda/etc/profile.d/conda.sh).
I also tried activating the environment using source rather than 'conda activate': !source /anaconda/envs/desired_env/bin/activate. This runs but doesn't actually do anything when I see the current environment in conda env list
Edit: also adding that if I install a package in the (base) environment in the terminal, I still don't have access to it in jupyter notebook.
I'm the PM that released AzureML Notebooks, you can't activate a Conda env from a cell, you have to create a new kernel will the Conda Env. Here are the instructions: https://learn.microsoft.com/en-us/azure/machine-learning/how-to-access-terminal#add-new-kernels

Unable to create and activate Python Virtual Environment using Jenkins Execute Shell [duplicate]

This question already has answers here:
Virtualenv Command Not Found
(31 answers)
Closed 1 year ago.
I am trying to create a python virtual environment for Django in ubuntu 20.04 using jenkins execute shell. I have seen a solution here at askubuntu and trying to reproduce this solution but getting the following error in console output:
Running as SYSTEM Building in workspace
/var/lib/jenkins/workspace/python_virtual_Env [python_virtual_Env] $
/bin/bash /tmp/jenkins8922469990510149005.sh ModuleNotFoundError: No
module named 'virtualenv.seed.embed.via_app_data'
/tmp/jenkins8922469990510149005.sh: line 3: cd: virtual_django1/bin:
No such file or directory
/var/lib/jenkins/workspace/python_virtual_Env
/tmp/jenkins8922469990510149005.sh: line 6: activate: No such file or
directory Build step 'Execute shell' marked build as failure Finished:
FAILURE
Below are my commands which i have used in jenkins execute shell:
#!/bin/bash
virtualenv virtual_django1
cd virtual_django1/bin
pwd
dir
source activate
I have ran the above commands with and without #!/bin/bash but still error persist. How can i get rid off this error in console and get able to create as well activate python virtual environment using jenkins execute shell.
I am able to resolve this issue by creating a virtual envirnoment with another command rather than virtualenv. Below is the exact shell commands which will be needed to create and activate the virtual envirnoment for python using jenkins shell:
#!/bin/bash
sudo apt install python3.8-venv
python3 -m venv Virtual_Django
cd my_env/bin
pwd
dir
source activate

Shell script start Python instance with conda env using macOS Automator

I try to automate some repetitive terminal commands on startup (OS X).
The internet provides some tweaks for .py-scripts, but not for python instances in a virtual environment. In my case I tried to start Airflow on startup, which is based in a virtual environment (airflow).
Normally I type:
#First Terminal
conda activate airflow
airflow scheduler
#Second Terminal
conda activate airflow
airflow webserver -p 8080
Do you have any solution how to run a macOS Automator script which covers the parameters?
The one liner
/Users/user_name/opt/anaconda3/bin/python conda activate airflow && airflow scheduler
doesn't work in my tries (via AppleScript, Run shell script, ...).
I always get an error like: no such file or directory: activate or *no such file or directory: airflow *
--> struggle by the blanks (airflow scheduler)

Starting with anaconda on Ubuntu 18.04

I installed latest version of Anaconda3-2020-02.
I was trying to follow this instruction in order to create environment for running with python==3.6 instead of python==3.7., because I have python 3.6. installed.
So, running the
conda create --name snakes python=3.6
and then activating my environment with conda activate snakes, it enters environment (snakes). However there are no anaconda packages inside like jupyter notebook or others, no anaconda-navigator... So whats the purpose of it and how can I run these programs from environment?
Also, for some reason (when I am not in the environment, just regular bash shell) $PATH is not set up to the /bin directory in anaconda, just to /condabin. Can you explain this also, because I am not able to run nothing except conda command from shell after recommended installation.
If by default after installation environment is not activated, you should activate it by sourcing a file anaconda3/bin/activate. You will see indicator (base) on the left of your bash prompt.
Good thing about anaconda3-2020-02 is that it is not messing with system python, as newest python is being launched inside environment and proper$PATH is set up just inside environment. If only, for some reason, specific version of python is needed, then it makes sense to set it up with this instruction. I would be just using default one with python 3.7 probably from (base) environment.
Keep in mind that by default anaconda components are not being set up inside new environment being created. In order to bring them, for example jupyter, you should run the command like this:
conda create --name snakes python=3.6 jupyter

How to activate a Conda Environment from the "sh" shell (on Nodejs)?

I can activate the Conda Environment with any problems from the bash shell. I can use this two options on bash:
source activate env_name
. activate env_name
But I am using the method exec of Node to run the activation of the environment. Node uses sh shell in order to run commands. I tried the commands above, but they did not work. I got this error
/bin/sh: 1: /env_name/bin/source: not found
So I had to use this command to run the environment
const child_process = require('child_process')
child_process.exec('bash -c "source activate env_name"')
Is there a better way to do this to make it work on Ubuntu?
I run this on Windows and it is enough to make it work
child_process.exec('activate env_name')
Conda doesn't do well right now (in conda 4.3.x) with shells that aren't "advanced" like bash and zsh. Pure posix dash and the busybox shells, for example, don't work right now.
Good new though... They soon will. Support for a larger variety of shells is a feature of conda 4.4. The base PR for this work was https://github.com/conda/conda/pull/5044, and there are several follow-ons.
Conda 4.4.0 should be released into canary in the next several weeks.

Resources