0

I have a python program in ~/bin called program.py. If i'm in that folder, I can run the program from the terminal without problems, but if I'm in another folder I get:

python: can't open file 'program.py': [Errno 2] No such file or directory

I have ~/bin in the PATH variable, and I can run my bash scripts without problems from whatever folder.

What is happening?

DATA:

in this folder I have my python program:

$ pwd
/home/pol/bin/Python
$ head -1 program.py
#! /usr/bin/python3
$ which python3
/usr/bin/python3
$ echo $PATH
/home/pol/bin:/usr/lib64/ccache:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin
$ program.py
bash: program.py: command not found
5
  • 1
    How are you trying to execute the program? it looks like you're doing something like python program.py - in that case, the shell only searches PATH for python Aug 25, 2020 at 19:26
  • Yes, I'm doing python program.py. In that case, how can I execute the program from another folder? it is not possible? Your answer explains, as I understand, why I'm not having trouble with the bash scripts, because I only have to run program.sh...
    – fich
    Aug 25, 2020 at 21:17
  • You give it an appropriate shebang and make it executable, as shown in Artem S. Tashkinov's answer - then execute it using just program.py Aug 25, 2020 at 22:34
  • I have done that with no success. I used the shebang Artem told me and only #! python3 too. I changed the permissions with chmod +x program.py and chmod 775 program.py either, but I can't get the thing working. I suppose maybe these things are not the problem, as I can efectively run the program if I'm in its folder.
    – fich
    Aug 25, 2020 at 22:54
  • Your program is in /home/pol/bin/Python while PATH only contains /home/pol/bin Aug 25, 2020 at 23:56

1 Answer 1

1

Do you have a shebang? Your first line of the script should point to the actual interpreter, e.g.

#!/usr/bin/python3

Also, make sure the permissions are set correctly, e.g. you'll probably want something like 775.

Here's to show it works just fine:

$ pwd
/tmp/test
$ ls
test.py
$ head -3 test.py 
#!/usr/bin/python3
# SPDX-License-Identifier: GPL-2.0 OR GPL-3.0
#
$ export PATH=/tmp/test:$PATH
$ test.py
usb1              1d6b:0002 09 1IF  [USB 2.00,   480 Mbps,   0mA] (xhci-hcd 0000:04:00.1) hub
usb2              1d6b:0003 09 1IF  [USB 3.10, 10000 Mbps,   0mA] (xhci-hcd 0000:04:00.1) hub
usb3              1d6b:0002 09 1IF  [USB 2.00,   480 Mbps,   0mA] (xhci-hcd 0000:04:00.3) hub
...
skipped

i.e. everything works.


The actual answer after getting all the info: your program is in /home/pol/bin/Python while $PATH only contains /home/pol/bin. Move the program to /home/pol/bin or add /home/pol/bin/Python to $PATH.

4
  • Yes I have the shebang and the permission are ok, I have done chmod +x program.py. The programa executes correctly if I'm in its folder, so all this stuff must be ok I suppose.
    – fich
    Aug 25, 2020 at 21:16
  • See my comment: if everything is properly set up, it must work for you. If it doesn't show your echo $PATH, show head -1 yourscript.py, show which python (or python2/python3) Aug 25, 2020 at 22:46
  • I have updated. Maybe I'm doing some dumb thing... sorry for that!
    – fich
    Aug 25, 2020 at 23:16
  • 1
    Thanks for the help Artem!
    – fich
    Aug 26, 2020 at 0:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .