Ask Your Question
0

Run a python script from any directory

asked 2013-11-07 22:11:27 -0500

shellwoo3 gravatar image

I'm a newbie and I am running Fedora 19. I have a python script called runScript.py located in /home/swooten/python directory. I would like to run this script from any directory. I edited the .bashrc and .bash_profile...input info below:

.bash_profile: I entered the bold text PATH=$PATH:$HOME/.local/bin:$HOME/bin:$HOME/python/runScript.py

.bashrc: I entered the bold text

User specific aliases and functions

CLASSPATH=$CLASSPATH:/home/swooten/python/runScript.py export CLASSPATH

When I run the ./runScript.py from /home/swooten I get the following error: bash: ./runScript.py No such file or diectory

Can someone please help me?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
2

answered 2013-11-07 22:30:36 -0500

skytux gravatar image

updated 2013-11-07 23:08:10 -0500

In the case of .bash_profile, it is the directory, not the file, that needs to be part of the path. It should be:

PATH=$PATH:$HOME/.local/bin:$HOME/bin:$HOME/python

Then, in order to run your script, you don't need the ./ before it, just use it as any other command (because now it is in your path):

$ runScript.py

NOTE: Instead of creating ~/python, you could copy your script to ~/bin, because it is already in yout path.

HTH,

Germán.

edit flag offensive delete link more

Comments

Your explanation is spot on but I'd like to mention also the ~/.local/bin directory which is also on the path. Personally I don't like "polluting" my home directory with folders so I place my scripts in the aforementioned path.

Felix Schwarz gravatar imageFelix Schwarz ( 2013-11-08 17:17:17 -0500 )edit
1

Just to make the answer complete - in order to be able to run your python script with runScript.py on the command line, apart from setting the PATH as described above, there are two more steps needed: 1. make sure the script is executable by the user: chmod u+x /home/swooten/python/runScript.py, 2. start the script with the correct shebang #!/usr/bin/env python (see http://en.wikipedia.org/wiki/Shebang_%28Unix%29), otherwise the script will be executed as a shell script

marcindulak gravatar imagemarcindulak ( 2013-11-11 04:44:50 -0500 )edit
-3

answered 2013-11-07 22:26:05 -0500

Rajat gravatar image

Copy runScript.py to /usr/bin/

sample code [root@rajat adt-bundle-linux-x86_64-20131030]# which ls alias ls='ls --color=auto' /usr/bin/ls [root@rajat adt-bundle-linux-x86_64-20131030]# ls -ltra /usr/bin/ls -rwxr-xr-x 1 root root 120232 May 8 2013 /usr/bin/ls [root@rajat adt-bundle-linux-x86_64-20131030]#

In above case "ls" command which run from any directory so for your "runScript.py" give equal permision as "ls" then try to the same.

edit flag offensive delete link more

Comments

putting anything custom in /usr/bin is not a good idea. That place should be reserved for Fedora's binaries from RPMs.

Felix Schwarz gravatar imageFelix Schwarz ( 2013-11-08 17:15:28 -0500 )edit

Question Tools

Stats

Asked: 2013-11-07 22:11:27 -0500

Seen: 5,863 times

Last updated: Nov 07 '13