Getting Python scripts to execute in RedHat

P

Pythor

I apologise if this is a stupid newbie error, but I've been googling
"hash bang" and "shebang" all morning. I've added the shebang to my
scripts:

#!/usr/bin/python

I've added execute permissions:

chmod +rx shebang.py

But I still can't execute my scripts by themselves

shebang.py ## produces error

python shebang.py ## runs correctly

I found one site that mentioned adding "./" to the beginning, and that
works.

../shebang.py ## runs correctly.

I gather that there's a path problem then. My script is in
~/pyscripts/. How do I get my scripts to run without using "./"?
 
D

Daniel Nogradi

I apologise if this is a stupid newbie error, but I've been googling
"hash bang" and "shebang" all morning. I've added the shebang to my
scripts:

#!/usr/bin/python

I've added execute permissions:

chmod +rx shebang.py

But I still can't execute my scripts by themselves

shebang.py ## produces error

python shebang.py ## runs correctly

I found one site that mentioned adding "./" to the beginning, and that
works.

./shebang.py ## runs correctly.

I gather that there's a path problem then. My script is in
~/pyscripts/. How do I get my scripts to run without using "./"?

Add ~/pyscripts to your path for example by putting this into your
..bash_profile if you are using bash:

PATH=$PATH:$HOME/pyscripts
export PATH

HTH,
Daniel
 
D

Dennis Lee Bieber

I found one site that mentioned adding "./" to the beginning, and that
works.

./shebang.py ## runs correctly.

I gather that there's a path problem then. My script is in
~/pyscripts/. How do I get my scripts to run without using "./"?

Typically, the "current directory" is not considered valid for
executables when searching for a program (under Linux). It's to prevent
things like an email attachment being saved in a user's home directory
taking control (ie, if one saves an attachment named "ls", and then
types "ls" in a shell window, the saved attachment is NOT run).

"./" is explicitly saying "the file to be executed is in the current
directory". If it was working for your test, you had to be /in/
"~/pyscripts/". Now, if you mean you store all your python programs in
"~/pyscripts/" but you want them to run from any directory, you need to
add "~/pyscripts/" to your login PATH definition. If you want to always
make the local directory valid, add the "./".
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
P

Pythor

Dennis said:
Now, if you mean you store all your python programs in
"~/pyscripts/" but you want them to run from any directory, you need to
add "~/pyscripts/" to your login PATH definition. If you want to always
make the local directory valid, add the "./".


Thank You. Between your explanation and the previous poster's
instruction, I've got it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top