# Run a python script from any directory

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 close merge delete ## 2 Answers Sort by » oldest newest most voted 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.

more

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.

( 2013-11-08 17:17:17 -0600 )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

( 2013-11-11 04:44:50 -0600 )edit

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.

more