Nothing happens when executing a python shebang script in /usr/local/bin/ - python

Nothing happens when executing a python shebang script in /usr/local/bin/
Hopefully someone can help me. So i made a simple python program called test for testing out shebang scripts(I have used chmod to make it executable):
#!/usr/bin/python
print "hello"
after i copied it to /usr/local/bin/ i tried to call it by typing in
my shell:test
but nothing happened...
(There were no errors)
Adrian

test is actually a shell builtin:
$ type test
test is a shell builtin
Rename your script to something else or run it directly by executing /usr/local/bin/test.

Blender is right: 'test' is an unfortunate name choice for your file. There is already a shell builtin function called 'test'. It would be the same if you tried to make a python script called 'ls'. The reason it works when executing './test' is due to the fact that './' tells the shell to make the current directory first in the executable path. If you rename your python script to 'bangtest' and make sure it has executable permissions (chmod +x bangtest), it will work in the manner you desire.

Related

Run terminal script in python?

I am using python and I am trying to run a shell script that is located in another folder I am trying
subprocess.call(['source','../Apps/appName/run'])
Where 'run' is a shell script I wrote and made an executable. But it keeps giving errors such as
No such file or directory or **No such file or directory: "source"
I have also tried the following
subprocess.call(['source','../Apps/appName/run'])
subprocess.call(['source ../Apps/appName/run'])
subprocess.call(['.','../Apps/appName/run'])
I am trying to run the script and leave it alone (as in I do not want any return value or to see what the output of the shell script is.
Thank you in advance
source is a shell builtin that reads a file and interprets the commands in the current shell instance. It's similar to C #include or Python import. You should not be using it to run shell scripts.
The correct way to run a script is to add a shebang like #!/bin/bash to the first line, and chmod +x yourscriptfile. The OS will then consider it a real executable, which can be executed robustly from any context. In Python, you would do:
subprocess.call(['../Apps/appName/run'])
If for whichever reason this is not an option, you can instead explicitly invoke bash on the file, since this is similar to what would happen if you're in bash and type source:
subprocess.call(['bash', '../Apps/appName/run'])

Executing a python script from the terminal in Unix

I have a python script and I can't figure how to execute it in the terminal. The script has the #!/usr/bin/python at the beginning, is executable and I've tried locating myself in the right directory and python name.py but what I want to print (ergo what the script says it should print) doesn't print in the terminal.
I feel I'm missing something... I just started with this so... Help!
If name.py is executable, you can run it using:
./name.py
As for the #!, it is better to use as a first line:
#!/usr/bin/env python
This way path to python interpreter is not hardcoded, the first python found in $PATH is used instead.
Also: it makes your script to run in fresh environment (you can see man env for more information).

Execute script in Python2 on Unix Command Line

Yes, I know I can do
python2 cal.py
What I am asking for is a way to execute it on the command line such as:
calpy
and then the command afterwards. I put in in a path and when I write cal.py in the command line:
/usr/bin/cal.py: line 5: print: command not found
I don't want to issue cal.py to run my script, I want it to be issued with calpy
I'm running Arch Linux if that helps, thanks. Sorry for my English.
In order for bash to know to run your script via the Python interpreter, you need to put an appropriate shebang at the start. For example:
#!/usr/bin/python
tells bash to run /usr/bin/python with your script as the first argument. I personally prefer
#!/usr/bin/env python
which is compatible with virtualenv. You also need to ensure that the permissions on your script allow it to be executed:
~$ chmod +x path/to/cal.py
Finally, in order to call cal rather than path/to/cal.py, you need to remove the .py extension and make sure that the directory containing cal is in your command search path. I prefer to add ~/bin to the search path by modifying the $PATH environment variable in ~/.bashrc:
export PATH=$HOME/bin:$PATH
then put my own executables in ~/bin. You could also copy (or symlink) cal to one of the system-wide binary directories (/bin or /usr/bin), but I consider it bad practice to mess with system-wide directories unnecessarily.
Ok, you need a couple of things for achive what you want.
First you have to tell your script "How" is going to execute/interpret it. You can do this writting
#/usr/bin/env python
at the very beggining of the file.
The problem you have is the system is trying to execute the script using bash. And in bash there is no print command.
Second you need give execution privileges to your script. And of course if you want to call your script through the command "calcpy", the script has to be called like that.
Put this (exactly this) as the first line of your script:
#!/usr/bin/env python

Why is Python Popen using a different executable

I have a program with two different versions on this computer. I can't get rid of the older one because I don't have root access, but I put the newer one first in 'bin' in my home directory (which is the first thing in my $PATH)
I tried calling it with Python's Popen
Popen(['clingo'...]...)
and it worked fine
But then I needed to set an environment variable before calling it so I renamed 'clingo' to 'run_clingo' in the bin directory and replaced it with a script:
# File: ~/bin/clingo
export LD_LIBRARY_PATH=/usr/local/gcc4.8.1/lib64:$LD_LIBRARY_PATH
run_clingo $#
When I run 'clingo' from terminal, it works fine, but Python Popen calls the older version (which is later on the PATH).
How can I get Popen to call the right one? Why does changing an executable to a script cause Popen to search elsewhere?
Add shebang #!/bin/sh at the very top of ~/bin/clingo and run:
$ chmod +x ~/bin/clingo
to make the file executable.
Popen() calls os.execvp() to execute a program. Its behaviour differs from your shell that emulates execvp(): if there is no the shebang (!#.. at the top) in the script then the shell reruns it as a shell script but Python's os.execvp() runs the next file in PATH instead.
Wild guess is that Popen does not use the same PATH as your terminal. I would check the shebang line (#!) sometimes people put env -i python there, which explicitly asks fro empty, i.e. default environment.
Note that subprocess.call is the recommended way, not using the Popen directly.

Executing python program

I have been searching the web for an answer now for quite a while, but this is giving me really headache:
I am using Ubuntu 12.04 and I want to execute a Python script from the terminal without using the full path.
So i added /home/kyril/python/scripts/ to the PATH variable through putting the following into ./bashrc:
kyrilpathvariable="/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/kyril/Python/scripts/:/home/kyril/Bash/scripts"
if [ "$kyrilpathvariable" = "$PATH" ]; then
echo PATH already exported
else
PATH=$PATH:/home/kyril/Python/scripts/
PATH=$PATH:/home/kyril/Bash/scripts/
export PATH
fi
(I know the if clause is not necessary but I did not like to have everything two times in my PATH if I type exec bash.)
Now the problem: this perfectly works for my Bash scripts, so after making them executable via chmod I can just type $ script.sh and it is executed. However if I type $ python3 script.py the following error is raised: python3: can't open file 'script.py': [Errno 2] No such file or directory
if I type in the full path to the script it works. Anybody has an idea what I am doing wrong? Do I have to add the directory to the PYTHONPATH? (As I understood this only helps for importing modules).
Thanks guys!
When invoking python3 directly, python runs the script file you told it to, without using $PATH to find it. PYTHONPATH is irrelevant--that's used for searching for Python modules.
I'm guessing you're having issues with the wrong interpreter getting invoked when you run script.py by itself. I don't know what the first line of your script is, but it should be this:
#!/usr/bin/env python3
Or if you need even finer control:
#!/usr/bin/env python3.2
And for Python 2 scripts:
#!/usr/bin/env python2
Or:
#!/usr/bin/env python2.7
You should check that these executables exist on your system before trying to use them.
I would guess that path variables are ignored when python searches for the input-file. Python starts searching for 'script.py' in the current directory, not knowing that there is a path variable declared for that file, and therefore cannot find it.
Unfortunately I'm not sure how to solve it but maybe someone more experienced with variables can enlighten us?
python3 $(type -P script.py)
Tells Bash to look in the PATH for the executable file and supply its location and name.
For example:
$ type -P script.py
/usr/local/bin/script.py
To avoid duplicate entries in the path, you can do:
for dir in Python Bash; do
dir_to_add="$HOME/$dir/scripts"
case ":$PATH:" in
*:"$dir_to_add":*) ;; # path already contains dir, do nothing
*) PATH+=":$dir_to_add" ;;
esac
done

Categories