How to run a shell script as source using python to set environment variables? - python-3.x

I created a shell script which is used to set the environment variables. I am trying to run this script via a python script but every time shows me error as "Command not found". I want to run the shell script as source for setting up the environment variables.
Python : 2.7.5
Script Name : abc.sh
Normal Execution on shell : source abc.sh
Tried using
python : os.system ("source abc.sh")
this shows an error as
"Command not found".
Anybody can help me out how to run this script successfully as source via python ?

The error is because source is a shell "builtin" command -- it is not an external command in your $PATH. But the more fundamental problem is that what you're trying to do won't work due to how environment variables work.
Env vars are private to each process. They are not global and a process cannot modify the env vars of a different process; at least not without the cooperation of the other process. When you start a process it inherits a copy of the env vars of the parent process or the parent provides an explicit set of env vars to the child process it spawns. In either event each process has its own, private, env vars. So even if you did
os.system("sh -c 'source abc.sh'")
it would only modify the env vars of the sh subprocess. It would not modify the environment of the python process.
The simplest solution is to start a shell, do the source abc.sh, then exec your python program. If you absolutely have to set the env vars by running a shell script from within your python program your script will have to write the vars to stdout. Your python program will then have to read that output and parse it to extract the env vars names and value then call os.putenv() to set each var in the python process.

Try your command in command prompt. This because you have not define os in the your command
python : os.system ("source abc.sh"). Your computer is unable access os.system. You first need to install the packages of python which os.system module in it.

Related

How to Run Python (with imported modules) from Applescript => Module Not Found Errors

How do I run a python script (with imported modules) from Applescript?
From terminal, these scripts work fine
python -m api.get_classrooms
python /absolute/path/to/project/api/get_classrooms.py
If I run the terminal script from AppleScript, I get module not found errors:
set pythonPath to "/path/to/anaconda3/envs/jupyter/bin/python"
set scriptPath to "/absolute/path/to/project/api/get_classrooms.py"
set result to do shell script pythonPath & " " & scriptPath & " "
I get the error
ModuleNotFoundError: No module named 'api'"
I did add my project to the $PYTHONPATH within .bash_profile
echo $PYTHONPATH => /path/to/project/a
And the python file (called by AppleScript) is set to executable
Is Applescript not aware of the PYTHONPATH?
Any push in the right direction is appreciated.
AppleScript's do shell script does not import the environment used by a typical interactive shell. This includes the $PATH variable, which I suspect is where your pro blew lies. You can coerce it into doing a full setup by calling the path_helper utility that apps like Terminal use. Try running this code:
set pythonPath to "/path/to/anaconda3/envs/jupyter/bin/python"
set scriptPath to "/absolute/path/to/project/api/get_classrooms.py"
set result to do shell script "eval `/usr/libexec/path_helper -s`; python -m api.get_classrooms"
Thanks to ted wrigley :) I was finally able to get it to work...
By not targeting the get_classrooms.py script directly.
Instead, AppleScript runs the below shell script (working within the project's subdirectory 'bash'. The bash script executes the python script. It wasn't the exact solution I wanted...but it works...and is (perhaps) more elegant....at least until the project is converted to a webapp with flask.
For anyone having trouble targeting python scripts with applescript applescript=>bash=>python
Applescript
do shell script "/Users/me/path/to/project/bash/applescript.sh"
Bash script within project/bash directory
#!/usr/bin/env bash
# eval /usr/libexec/path_helper -s; # didn't need
export PYTHONPATH='/Users/me/path/to/project';
/Users/me/anaconda3/envs/jupyter/bin/python -m api.get_classrooms

Defining shell environment variables in tcsh [duplicate]

This question already has answers here:
Can a shell script set environment variables of the calling shell? [duplicate]
(20 answers)
Closed 3 years ago.
Unable to create environment variables using a tcsh script.
Tried set, but works only inside the script.
setenv doesn't work outside the script.
export says "command not found" in the terminal I'm trying to run.
#!/usr/intel/bin/tcsh
#set WV "/p/hdk/cad/custom_waveview/O-2018.09-SP2/bin/wv"
setenv WV "/p/hdk/cad/custom_waveview/O-2018.09-SP2/bin/wv"
echo $WV
env $WV "/p/hdk/cad/custom_waveview/O-2018.09-SP2/bin/wv"
I expect the output to be /p/hdk/cad/custom_waveview/O-2018.09-SP2/bin/wv, when i echo the environment variable WV on the terminal, but i am getting the error of undefined variable.
Environment variables are set in the current process and inherited by child processes. You can't set environment variables in a parent process.
You have to use the source command to execute the script. That makes the current shell process execute the script itself, rather than running it in a child process.
source env_vars.tcsh
set is for setting shell variables, not environment variables. export is a bash command (and also other shells based on Bourne Shell syntax), not a tcsh command.
env requires the arguments before the program name to be variable settings in the form name=value, e.g.
env VAR1=val1 VAR2=val2 /p/hdk/cad/custom_waveview/O-2018.09-SP2/bin/wv
It runs the program with those variables added to the environment.

Setting console env using a shell script

I have a shell script setmyenv.sh as below
#!/bin/sh
export PATH=./abc/tools:$PATH
env | grep PATH
When I run it sh setmyenv.sh, I could see that the PATH env is set accordingly.
PATH=./abc/tools:<whatever my existing PATH setting>
However, after my command finish, if I manually type env | grep PATH on the console, I got
PATH=<whatever my existing PATH setting>
I lost the setting that I set using setmyenv.sh
It looks like the environement is only set in the lifetime of my script run.
How could I have the environment set sticky even after the script ended. i.e. the purpose of the script is to set the environment.?
P/S: I don't want to set it in my .bash_profile nor etc\profile, given I only want to set it when needed, by calling setmyenv.sh, but not every time I open my console. i.e. not per the answer of Using .sh script to set an environment variable or How to set global environment variables using shell script .sh
When you run
sh setmyenv.sh
it runs in a separate sh process and the changes to PATH are lost when the process finishes.
You need to source your script:
source setmyenv.sh
or
. setmyenv.sh
so that it runs in your current shell and all variable assignments are preserved. Remember not to have any exit in setmyenv.sh script. If you do, sourcing the script will terminate your shell.
See also:
Difference between sourcing a script vs executing it
What's a subshell

Anaconda activate

I am using anaconda python. So every time, in my mac terminal, I input the terminal command:
source /Users/mylaptop/anaconda/bin/activate /Users/mylaptop/anaconda
And then I activated the anaconda python environment. But I don't want to write this command line every time, so I tried a bash script like this:
#! /bin/bash
source /Users/mylaptop/anaconda/bin/activate /Users/mylaptop/anaconda
and I put this file in the directory /usr/local/bin. But unfortunately, I cannot log into anaconda environment in this way. There is no error message showed up in the terminal. So I do not know what is happening here.
Could anyone help me out?
The easiest fix is to just put /Users/mylaptop/anaconda in your PATH, by adding something like
export PATH="/Users/mylaptop/anaconda:$PATH"
to your bash profile (~/.profile).
You can't put the activate script in a script because it has to be "sourced" to work. source causes the script to be run in your current shell (as opposed to a subshell, which is how the bash script you wrote is run). This is necessary because it modifies your PATH environment variable, and environment variables from your current shell cannot be modified by subshells.

Why doesnt "export" works in linux system() cmd to set a linux shell environment variable at runtime?

I was trying to set a linux shell environment variable at run time in software using system() command. In the target, couldn't see it set.
for eg.
snprintf(buf, sizeof(buf), "export A=%s", "luck");
system(buf);
Tried /bin/sh -c "export A=B" in the target run time and had no success.
The system call starts a new shell and runs the command in there.
The environment variable will be set in that shell only.
To set an environment variable in your own process, use putenv.

Resources