4

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!

1
  • 1
    Why do you want to run python3 script.py instead of script.py? Commented May 5, 2012 at 10:55

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I had #!/usr/bin/python3, so this fixed my problem!
0

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?

Comments

0
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

Comments

0

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

1 Comment

Thanks! That is a little bit more elegant than what I did :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.