printing from bash if virtualenv is activated - bash

I want to print in the console if virtualenv is activated.
Given that activated environment is "myvirtualenv", code should be like this:
echo some_function
and result will be
myvirtualenv

virtualenv's activation script sets "VIRTUAL_ENV" environment variable. Just print it:
echo "$VIRTUAL_ENV"

Related

Problem modifying PATH variable in fish config

I have the following configuration in ~/.config/fish/conf.d/python.fish:
# Initialise pyenv if found
echo "Running python config"
if status --is-interactive && test -d "$HOME/.pyenv"
echo "Inside pyenv if"
set -pxg PATH $HOME/.pyenv/bin $HOME/.pyenv/shims
source ("$HOME/.pyenv/bin/pyenv" init - | psub)
echo "End pyenv if"
end
# Poetry settings
if status --is-interactive && test -d "$HOME/.pyenv"
set -xg POETRY_VIRTUALENVS_IN_PROJECT 1
set -xg POETRY_VIRTUALENVS_CREATE 1
set -pxg PATH $HOME/.poetry/bin
end
echo "End python config"
Every single echo command in the configuration is executed when I am creating a new shell, but the PATH variable is not modified. However, the POETRY_ variables show up as expected.
But things work as expected if I source the file in an existing shell with
source ~/.config/fish/conf.d/python.fish
What could possibly be wrong here?
Update: The problem only occurs inside of tmux, and not when I am starting terminals like Alacritty or Kitty. But all echo commands is still run inside tmux.
This was a Debian bug - they added configuration that reset $PATH, and it happened after the user's configuration snippets but before config.fish.
This appears to be fixed in Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1000199. Now they merely add the default directories instead of replacing $PATH entirely.
Because config.fish is read after all the conf.d files, you can always use config.fish to override anything done in the snippets, like if your vendor breaks things.

Setting Environmental Variables Through a BASH Script in Jupyter

I am attempting to execute a BASH script which sets needed environmental variables from within a Jupyter notebook. I understand that the magic command %env can accomplish this, but the BASH script is needed in this instance. Neither the use of !source or %system accomplishes the goal of making the environmental variables persist within the Jupyter notebook. Can this be done?
You could use python to update os variables:
Cell
! echo "export test1=\"This is the test 1\"" > test.sh
! echo "export test2=\"This is the test 2\"" >> test.sh
! cat test.sh
Result
export test1="This is the test 1"
export test2="This is the test 2"
Cell (taken from set environment variable in python script)
import os
with open('test.sh') as f:
os.environ.update(
line.replace('export ', '', 1).strip().split('=', 1) for line in f
if 'export' in line
)
! echo $test1
! echo $test2
Result
"This is the test 1"
"This is the test 2"
To permanently set a variable (eg. a key) you can set a Bash environment variable for your Jupyter notebooks by creating or editing a startup config file in the IPython startup directory.
cd ~/.ipython/profile_default/startup/
vim my_startup_file.py
The file will be run on Jupyter startup (see the README in the same directory). Here is what the startup .py file should contain:
1 import os
2 os.environ['AWS_ACCESS_KEY_ID']='insert_your_key_here'
3 os.environ['AWS_SECRET_ACCESS_KEY']='another_key'
Now inside a Jupyter notebook you can call these environment variables, eg.
#Inside a Jupyter Notebook cell
import os
session = boto3.session.Session(
aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID'),
aws_secret_access_key=os.getenv('AWS_SECRET_ACCESS_KEY'),
region_name='us-east-1'
)
You will need to restart your kernel for the changes to be created.

Unable to run an interactive shell script using conda run

I have this script "/home/prod/test.sh"
#!/bin/bash
echo Hello, who am I talking to?
read varname
echo It\'s nice to meet you $varname
I was expecting that when I run this command
conda run -n myenv /home/prod/test.sh
I will see a prompt waiting for me to enter the value for varname , instead I got
(base) prod#dev-box:~$ conda run -n reportz /home/prod/test.sh
Hello, who am I talking to?
It's nice to meet you
(base) prod#dev-box:~$
Would very much appreciate a response detailing, how I can use conda to run an inetractive shell script ?
[Edit]
Replaced read varname with read -r varname < /dev/tty
It did wait for my input, but without displaying the output resulting from the echo statement echo Hello, who am I talking to?
Solution I'm looking for - the script should run with conda run command exactly as it would in a typical shell env.
The conda run command defaults to buffering I/O. To have interaction, one needs to include the --no-capture-output flag.
conda run -n myenv --no-capture-output /home/prod/test.sh
This feature was introduced in Conda v4.9.0 (Change Log).

How can I pass command line arguments when activating a conda environment in a shell script

I am on Linux, Python 3.6.
I have a shell script that looks like this:
#!/bin/bash
instance=$1
export instance
echo "Instance is" $instance
. /home/xyz/setenvvars.sh
source activate myenv
echo arg1 $1
echo env $instance
python myprg.py $1
python myprg.py $instance
It seems after I activate the conda environment, the command line argument this shell script received is not available in the activated environment. How can I pass the command line argument this script originally received to newly activated environment. The 2 echos after the activate show blanks.
TIA!

Run source in bash execution

Quick run down of what I'm doing, I'm making python development environments on my computer using virtualenv. I'm not one to remember all these commands, so I like to build simple(noob-ish) script that will help me.
Problem
As you see on step 2 in the Basic Usage section of the documentation. I need to run a command:
$ source venv/bin/activate
This activates my python environment. So I've tried a mixture of functions and eval. As mentioned, I am a bit of a noob when it comes to bash scripts.
My Code
File: fpyenv
#!/bin/bash
# $ cd ~/scripts/
# $ chmod u+x <filename>.sh
activateSrc(){
eval '$(source "${1}"/bin/activate)'
}
if [[ $1 == '' ]];
then
printf "ERROR: Missing Filename\n"
else
printf "Creating Virtual Environment $1\n"
# This creates the environment
virtualenv $1
printf "Do you want to activate $1 as your virtual environment?(y/n)\n"
# Get answer from user
read answer
if [[ $answer != 'y' ]];
# Said No
then
printf "Did not set $1 as your virtual environment\n"
else
# Said Yes
activateSrc $1
printf "Set $1 as your virtual environment\n"
fi
fi
This is what it should look like:
Step 1
myComputer $ fpyenv venv
returns
Creating Virtual Environment venv
Do you want to activate venv as your virtual environment?(y/n)
Step 2(user inputs y/n)
y
returns
Set venv as your virtual environment
(venv)myComputer $
But what I'm getting is:
Set venv as your virtual environment
myComputer $
So it doesn't run the source venv/bin/activate. Does anyone know what I'm doing wrong? I've looked through many answers, but given that source is commonly used in a different way in bash scripts, the answers I'm getting are no help. Thank you in advance!
FIX:
Change activateSrc to:
activateSrc(){
source $1/bin/activate
}
And execute script like this:
myComputer $ . fpyenv venv
It runs source just as written. The thing is that you need to source this new script as well, otherwise it just runs in a subshell and any changes made by the sourced script are lost when the subshell exits.

Resources