How to get path to the installed GIT in Python? - python

I need to get a path to the GIT on Max OS X 10.6 using Python 2.6.1 into script variables. I use this code for that:
r = subprocess.Popen(shlex.split("which git"), stdout=subprocess.PIPE)
print r.stdout.read()
but the problem is that output is empty (I tried stderr too). It works fine with another commands such as pwd or ls.
Can anyone help me with that?
UPDATE: When I run which git from Terminal it prints out path as expected. So, which can find it.
UPDATE 2: I just created the bash script
#!/usr/bin/env bash
GP=`/usr/bin/which git`
PWD=`pwd`
echo "PATH IS: ${GP}"
echo "PWD IS: ${PWD}"
and output is
PATH IS:
PWD IS: /Users/user/tmp

All which does is iterate over the directories in $PATH, checking to see if the file is there. Just write a small method to do likewise.

Related

script.py in path but can't be executed without providing full path

I have a script I have downloaded and installed from GitHub that I am trying to add to my path. Although the script is in the path already somehow I can't run it unless I provide the full path.
echo $PATH
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/Users/me/bin/SCRIPT_FOLDER/
/Library/TeX/texbin
/Users/me/bin/SCRIPT_FOLDER/script.py
Since I am working on Catalina 10.15.7, I saw in other answers that I need to add the script to the .zshrc file, so I did so.
code .zshrc
export PATH=/Users/me/bin/SCRIPT_FOLDER/:$PATH
export PATH=$PATH:/Users/me/bin/SCRIPT_FOLDER/script.py
Despite this the script cannot be run except if I type the entire path
python /Users/me/bin/SCRIPT_FOLDER/script.py --h
What can I try to solve this?
script.py must be executable and the first line must be as shown below.
? echo $PATH
.:./bin:...
? ls
bin
? ls -l bin/script.py
-rwxr-xr-x. 1 x x 40 Mar 9 10:54 bin/script.py
? cat bin/script.py
#!/usr/bin/python
print('Hello world')
? script.py
Hello world
?
If the script is not executable use the chmod command like so:
chmod 755 bin/script.py
Like most command-line programs, Python doesn't consult your PATH for the arguments (e.g., script.py, given the command python script.py). Your PATH only matters when deciding how to resolve the command name (in this case, python). If script.py is in one of your PATH directories (like /Users/me/bin/SCRIPT_FOLDER/), it has the appropriate executable bit set, and it begins with an appropriate shebang line such as #!/usr/bin/env python, then you can run it with the command script.py.

sh: 1: python: not found when execute python script from php

I'm tring to execute a python script call from php with the command below:
$output = shell_exec('python /var/www/html/sna/server/userManagement.py '. $user.' '. $pass .' \''.$action.'\' 2>&1');
But when I execute it I get this
sh: 1: python: not found
But python is correctly installed in my env.
If I digit
type -a python
I get the path of python in this env like below (not sure because they are two)
python is /home/leonardo/miniconda2/bin/python
python is /home/leonardo/miniconda2/envs/sna/bin/python
At the very beginning of the python script I have include
#! /usr/bin/env python
But I recieve always the same error. How can I solve ?
EDIT
I tried to add python path to the $PATH with command
export $PATH:/home/leonardo/miniconda2/envs/sna/bin/python
But I get the same error anywhay
Your binary is not in the PATH for the webservers user account.
PHP inherits the PATH from Apache. And most distros set a fairly restrained:
SetEnv PATH /bin:/usr/bin
Either change that, or putenv() in PHP, or use absolute paths instead.

python3.7 subprocess failed to delete files for me

I have a python script using 'subprocess' running linux command to confirm my task is doing the right thing, and it worked well. But i found that at the same time it will generate some log files when running my task. So i added a clean up function to rm log files for me at the beginning. My script is:
def test_clean_up_logs(path_to_my_log):
regex = path_to_my_log + ".*" # i need this because log will append current date time when it's generated
print(regex) # i can see it's correct
result = subprocess.run(['rm', '-rf', regex])
def test_my_real_test():
# This will run my real test and generate log files
but it turns out it did not remove log files for me after i added first test, it still have more and more logs file in my build dir. I run it using:
Python3.7 -m pytest /path/to/mydir
My question is:
1. Why did not it work? In my second test case, i am using 'subprocess' to run a linux command and it worked fine.
2. Is this correct way to clean up log files? i cannot think of a better way to do it automatically. Thanks!
Why did not it work?
Because the arguments that you gave to your command is passed in quotes and wildcards like * does not work in quotes. Currently the executed command looks like this:
$ rm "-rf" "filename.*"
Try this in your terminal and you will see that it will not remove the files that starts with filename..
You need to pass shell = True to execute the command in a shell interpreter and give your command as a single string.
subprocess.run(f'rm -rf {regex}', shell=True)

Execute a python script as a command

I'm writing a python script which will be placed in a location. I want to execute it just like a command. for ex.
$ find_branch test
where find_branch is a script placed in anywhere in the system.
I would like to know how to achieve this. I can run it on the place where the script is present by chmod u+x on the script and removing the .py from the script
sudo nano /usr/bin/testpyscript
Then inside the script:
#!/usr/bin/python
print("I'm a python script")
Give it x permission:
sudo chmod +x /usr/bin/testpyscript
Now you can use it as a regular command:
bash-4.2$ testpyscript
I'm a python script
It doesn't have to be exactly at /usr/bin, any location that is inside your $PATH will do. Let's say you want it to be located at some folder inside your home directory, you could do something like this:
pwd
/home/brunorb
mkdir somedir
sudo mv /usr/bin/testpyscript somedir/
export PATH=$PATH:/home/brunorb/somedir/
testpyscript # from any folder in the system
I'm a python script
Make sure python has been added to your path and #!/usr/bin/python is located at the top of your script.
Note You could just try adding your script to your /usr/local/bin/ directory and give it the proper permissions.
sudo cp <your script> /usr/local/bin/
You have a number of options on how to achieve this.
Add the location where you put the script to your PATH environment variable, for example in your ~/.bashrc script:
export PATH="${PATH}:/folder/where/you/put/the/script"
Install the script to a location that is already on your path. It does not have to be a system folder like /usr/bin. Many default Bash setups will include ~/bin in your PATH.
Give the full path to your script on the command line:
/folder/where/you/put/the/script/find_branch test
Run the script through Python. This is very similar to option #2:
python /folder/where/you/put/the/script/find_branch test
Create an alias for the script in your environment. In bash you would do something like the following in your ~/.bashrc:
alias find_branch='/folder/where/you/put/the/script/find_branch'
OR
alias find_branch='python /folder/where/you/put/the/script/find_branch'
For options #1, #2, #3 and #5a to work properly, you should have a shebang with the version of python as the first line of the script. Any of the following will do, depending on how you have/want your environment set up:
#!/usr/bin/python
#!/usr/bin/python2
#!/usr/bin/python3
#!/usr/bin/env python
#!/usr/bin/env python2
#!/usr/bin/env python3
Finally, you do not have to remove the .py extension from the script if you do not want to. Many bash scripts have a .sh extension, for example, which does not prevent them from running as-is. You just have to include the extension in the name of the script when you run it.

Running "IDLE3.2 -s" from the "Finder" in OS X 10.6

I want to run IDLE3.2 with the argument "-s" so it can read ".pythonstartup" and export relevant modules, change the working directory and etc. Here is what I have tried:
Created a shell script:
/usr/local/bin/idle3.2 -s
this works allright, however running the script from the Finder opens up the Terminal, which is not the desired behavior.
Created an applescript:
do shell script "/bin/bash; cd /usr/local/bin/; ./idle3.2 -s"
this get rids of the terminal however fails to pass "-s" argument to idle3.2 so the configuration file is not loaded.
any suggestions?
EDIT: turns out environment variables are not properly set even though /bin/bash is called. so the following solves the problem:
do shell script "/bin/bash; source ~/.profile; /usr/local/bin/idle3.2 -s"
I think your do shell script "/bin/bash; cd /usr/local/bin; ./idle3.2 -s" is doing extra work, and can probably be done more simply. Try:
do shell script "/usr/local/bin/idle3.2 -s"
thanks to #lain the following applescript solves the problem:
do shell script "source ~/.profile; idle3.2 -s"
where ~/.profile points the shell (in this case /bin/sh) the path for .PYTHONSTARTUP and the path for idle3.2

Categories

Resources