1

I have a basic problem where I don't know how to run a Python script from command line in Ubuntu without using python keyword. So, I put a shebang in my Python script so I could run it as nameofthescript from the command line, but I only could do it by using ./nameofthescript. I want to be able to run it by just typing the name of the script in the cmd. I searched and tried everything I could on the web, but none is working. Any help is appreciated. Below is a simple code I wrote to test it.

I already tried chmod +x this file. Also this file is saved with no extension.

#!/usr/bin/python

import sys

def main(argv):
    print(argv)
    print("Hello")

if __name__ == "__main__":
    main(sys.argv[1:])
L3viathan
  • 26,748
  • 2
  • 58
  • 81
Vecheka
  • 53
  • 1
  • 1
  • 3
  • You need to put the script on your `$PATH`, by putting it in a directory listed by typing `echo $PATH` on the commandline. – L3viathan Apr 16 '19 at 18:28
  • I have tried that, and still couldnt get it to work. Thank you for your comment. – Vecheka Apr 16 '19 at 18:33

4 Answers4

1

The problem is with your $PATH variable.

When you go to run a command (without the "./" in front of it) Ubuntu looks in all the folders listed in your $PATH variable. Your can see it by running:

echo $PATH

If Ubuntu doesn't see the command in any of those folders, it will say that it can't be found.

You can solve this problem by altering your $PATH variable in your profile. Go to your home directory and open the ".profile" file (note the period in front) and add the following to the end:

PATH = "/path/to/folder/with/file/:$PATH"

However, if it's a program you could see yourself using a lot in the future and your don't want to clutter up your $PATH, I'd recommend sticking the finished command in your "/usr/local/bin" folder instead. I find that folder gets used as an "odd sock drawer" of programs you create/compile yourself, so I usually end up putting my personal tools in there rather than modifying my $PATH.

TripleD
  • 199
  • 1
  • 15
  • That is very useful to know, thank you for pointing that out. I have tried putting my exec file in /usr/local/bin, but in the cmd when I typed nameofscript,nothing happened. However, like I have mentioned, when I typed ./nameofscript, the codes ran. – Vecheka Apr 16 '19 at 18:55
  • Interesting. Just for clarification, when you say “nothing happened” do you mean that there was no output at all and it waited for the next command, or was there a “command not found” message of some kind? Also, what output do you get from “echo $PATH”? – TripleD Apr 16 '19 at 21:15
  • Yeah, it just waited for the next command line. It does not print anything for me. This is the output I got from running echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin – Vecheka Apr 16 '19 at 21:21
  • Okay so the path is good. I tried running code on my machine and it worked fine. Try putting a “print(‘Hello’)” immediately after the import statement. If it prints “Hello”, then we know the OS is finding the file I’d suspect there may be a typo in your original code that is not in the lines you posted in your question. – TripleD Apr 16 '19 at 22:20
0

That's how it's supposed to work, not only for Python scripts but for any executable. See: Why do you need ./ (dot-slash) before executable or script name to run it in bash?

Jussi Nurminen
  • 2,257
  • 1
  • 9
  • 16
0

Please Try this one

On unix systems, Python scripts can be made executable using the following process:

Add this line as the first line in the script:

#!/usr/bin/env python

At the unix command prompt, type the following to make myexe.py executable:

$ chmod +x myexe.py

Move myexe.py into your bin directory, and it will be runnable from anywhere.

$ cp myexe.py /usr/bin

OR

$ cp myexe.py /usr/local/bin

So myexe.py

#!/usr/bin/env python
print("Hello This is executable python script")

Now Go to terminal and type myexe.py

$ myexe.py
Hello This is excutable python script

If you want to run by double-clicking remove .py extention

source link

bkawan
  • 1,171
  • 8
  • 14
  • Thank you for answer. I tried that, and I still have use ./ in front of the script to run it. When I tried running by typing the name of the script only, nothing happens in the terminal. – Vecheka Apr 16 '19 at 19:06
0

I have found a way to solve this. I still include the shebang #!/usr/bin/env python3.6 at the top of Python script. Then I would go to cd /etc->sudo nano bash.bashrc, and at the very last line, all I did was add a line (alias nameofscript = "./nameofscript"). From there I restarted my Ubuntu, and was able to run my Python script just by the name of the script. Thank you everyone for the help.

Vecheka
  • 53
  • 1
  • 1
  • 3