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.
Related
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
This question already has answers here:
Can I export a variable to the environment from a Bash script without sourcing it?
(13 answers)
Closed 3 years ago.
I have created a virtual environment on my debian system and i made a script that activates it (should).
However when i execute the script nothing shows up, not even an error, my guess is that it is running in a different shell or something but I don't know how to solve it.
Here is the code of the script
#!/bin/bash
source ~/PythonEnv/environments/my_env/bin/activate
I have changed the permissions already with chmod u+x, so that is not a problem.
When i execute the script nothing shows up at all. Any thoughts???
Add set -x at the beginning of your bash script will do the trick.
-x Print commands and their arguments as they are executed.
You can see more bash options here
http://linuxcommand.org/lc3_man_pages/seth.html
Adding x-permissions is not necessary, since you are using source with an absolute path. Of course this sets the environment only which is executed by the shell script which you have posted here. If you want the changes in your interactive shell, it is pointless to do it inside a script. You have to source the activate script in your shell (respectively inside that process where you want the environment to be modified).
I'm running Ubuntu on Windows Subsystem for Linux, and I have a bash script that I want to run that needs to access system environment variables. Specifically, I defined an environment variable on my system with the following command:
export SLACK_LEGACY_API_TOKEN="Insert Token Here"
I then define a file called slack.sh that looks like this:
#!/usr/bin/env bash
echo $SLACK_LEGACY_API_TOKEN
I then run this script with source ./slack.sh. When I run this command, it just prints out a blank line - it's not getting the value of the environment variable.
I've tried different syntax in the .sh file for referencing the environment variable, like "$SLACK_LEGACY_API_TOKEN" and ${SLACK_LEGACY_API_TOKEN} but same result. I've also run the script with /slack.sh and . ./slack.sh but same results.
How do I get my script to see $SLACK_LEGACY_API_TOKEN?
As mentioned by #chepner in the comments within my question, the issue was caused by defining the .sh file in my Windows environment. This results in the file being saved with DOS line endings which was causing the issues with the script. Redefining the file entirely within the Linux environment solved the problem.
Yesterday I ran into the git execute bit bash script quirk - the one that requires:
git update-index --add --chmod=+x scriptname.sh
and it seemed strange to me that it was even possible to get stuck in this situation. (Ie having created a script file that you don't have permission to run).
If I have created a shell script - surely I can run it under the permissions of the shell execute permissions. Why would it need it's own execute permission bit?
My question is: Why does a bash script require an execute bit if a windows batch script can just be executed?
To run a script you have two options in unix like systems. First Option is to use a direct interpreter call with the script as parameter.
# run a bash script
bash test.sh
# run a python scripts
python test.py
The second option is mark your file as executable, with the execute bit and after a call like this ...
# sample bash
./test.sh
# sample python
./test.py
... your system tries to find the right interpreter for you. For this the first line 'shebang' of the script is used.
Bash example:
#!/bin/bash
# points to the installed bash interpreter - bash example
Python example:
#!/usr/bin/python
# points to the installed python interpreter
To your question windows only use the file extension to detect a executable file.
Well, Linux is not Windows. Linux/Unix file systems support the executable bit to distinguish executable from pure data files, and to control exec permissions for user|group|others. You can still run the script if you prefix it with the name of the shell/binary you want to start it with, but if you want to do ./scriptname.sh or execute it from the path it needs to be flagged as executable for you as the onwer|a group member|some other user, and for scripts usually the shebang in the first line that defines the interpreter to start the script with: #!/bin/bash.
I am trying to open a terminal and run a script using matlab. The script will open an ssh connection. The matlab command is:
system(['lxterminal -e "bash ' scriptName '" &'],'-echo');
When I execute the matlab command the script runs but fails fails to validate SSL credentials.
The script is running ssh through the python paramiko package.
The error arises from the cli.py module.
The problem is solved if I run
system(['lxterminal -e "sudo bash ' scriptName '" &'],'-echo');
but then I have to enter the user password each time I execute the script.
If I open an lxterminal and run the same command:
bash scriptName
it works without the sudo.
I think it is related to some environmental variables / configuration which are not loaded in lxterminal before running the script, but cannot figure out it.
Using xterm instead of lxterminal has the same behavior.
Any ideas?
The fix, might be dirty, was to empty the LD_LIBRARY_PATH from the matlab environmental variables before calling the system command using the following command in the matlab script
setenv('LD_LIBRARY_PATH');
Probably the matlab LD_LIBRARY_PATH path is using obsolete libraries compared to the ones python needs.
A better approach might be to start removing one by one the paths until finding the one causing the problem.