3

Yes, I know I can do

python2 cal.py

What I am asking for is a way to execute it on the command line such as:

calpy

and then the command afterwards. I put in in a path and when I write cal.py in the command line:

/usr/bin/cal.py: line 5: print: command not found

I don't want to issue cal.py to run my script, I want it to be issued with calpy

I'm running Arch Linux if that helps, thanks. Sorry for my English.

3 Answers 3

4

In order for bash to know to run your script via the Python interpreter, you need to put an appropriate shebang at the start. For example:

#!/usr/bin/python

tells bash to run /usr/bin/python with your script as the first argument. I personally prefer

#!/usr/bin/env python

which is compatible with virtualenv. You also need to ensure that the permissions on your script allow it to be executed:

~$ chmod +x path/to/cal.py

Finally, in order to call cal rather than path/to/cal.py, you need to remove the .py extension and make sure that the directory containing cal is in your command search path. I prefer to add ~/bin to the search path by modifying the $PATH environment variable in ~/.bashrc:

export PATH=$HOME/bin:$PATH

then put my own executables in ~/bin. You could also copy (or symlink) cal to one of the system-wide binary directories (/bin or /usr/bin), but I consider it bad practice to mess with system-wide directories unnecessarily.

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

Comments

2

Ok, you need a couple of things for achive what you want.

First you have to tell your script "How" is going to execute/interpret it. You can do this writting

#/usr/bin/env python

at the very beggining of the file.

The problem you have is the system is trying to execute the script using bash. And in bash there is no print command.

Second you need give execution privileges to your script. And of course if you want to call your script through the command "calcpy", the script has to be called like that.

1 Comment

I got it to work with another comment. (For you future readers: Arch does NOT have Python Env. Install with pacman first.)
1

Put this (exactly this) as the first line of your script:

#!/usr/bin/env python

Comments

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.