AS_Butler
AS_Butler

Reputation: 281

Call a python script with a shebang

I want to run a python script foo.py from the command line like this

$ foo

Using a shebang in foo.py, for example:

#!/usr/bin/env python
print('this is foo')

allows me to call it like this:

$ ./foo.py

How do I drop the leading ./ and the trailing .py?

Upvotes: 0

Views: 514

Answers (1)

mri1939
mri1939

Reputation: 306

First, rename the file from foo.py to foo.

Then, move the file to /usr/local/bin/ or /home/user/.local/bin if the script will only be executed by a single user. Instead, if your script is placed somewhere in the system for example "/path/to/foo", you could add your "/path/to/foo" to the $PATH variable.

After opening a new terminal session. You should be able to execute the script without the "./" and ".py".

By the way "./" means that you want to execute a file in the current working directory. It is always possible to execute a file using a full path of the file, for example "/usr/bin/something_to_run".

Please consider reading about PATH variable here.

Upvotes: 1

Related Questions