8

I have a very simple Python question, with examples using Django. When running a Python script, do I always have to precede the script filename with the python command?

In the Django tutorial I am following, some commands are as follows:

django-admin.py startproject mysite

However, other are like this:

python manage.py runserver

Why does the top one not need the python command?

Alternatively, if my system knows that all Python scripts are to be executed by my python interpreter, why does the bottom one need the python command at all?

2
  • I believe django-admin.py should have a shebang at the top that says to implicitly run the python interpreter. Commented Feb 17, 2014 at 17:26
  • @austin That's correct, but manage.py is also auto-generated with the shebang. It does't have executable flag set, though. Commented Feb 17, 2014 at 17:46

2 Answers 2

3

The answer lies in a combination of two things:

  1. The shebang, the first line of a file.
  2. The permissions on the file, namely whether the executable flag is set or not.

Consider the first line of django-manage.py. On my system it is at /usr/local/bin/django-admin.py and the first line is:

#!/usr/bin/python2.7

And the permissions on the file:

-rwxr-xr-x 1 root root 127 Jan  9 13:38 /usr/local/bin/django-admin.py

The shebang tells my OS that I want to execute the file with the interpreter at /usr/bin/python2.7. The x characters in the permissions say that it is an executable file. I don't need to specify python beforehand, because the OS can figure out that stuff automatically from the above pieces of information.


Now for a freshly created manage.py, which I made by running django-admin.py startproject mysite, it looks like this:

#!/usr/bin/env python

And the permissions:

-rw-r--r-- 1 wim wim 249 Feb 17 17:33 manage.py

The shebang indicates to use whatever python interpreter my env is pointing at, so that part is in place already. But the file lacks executable permissions, so just running ./manage.py won't work (yet).

I can make it behave the same as django-manage.py by explicitly setting the executable flag using chmod +x manage.py. After that, you should see the x flags set when you do ls -l in the directory, and you should be able to run the file without specifying python beforehand.

Note: you do still have to use ./manage.py not just manage.py, this discrepancy is because django-admin.py lives in /usr/local/bin/ which is contained in PATH whereas the manage.py file likely doesn't. So we explicitly specify the path at the shell, where . refers to the current directory.

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

1 Comment

Probably worth being explicit that the given manage.py would be executable if-and-only-if given +x bits.
1

You can make the script executable by using chmod +x script.py, but you need the shebang line, usually #!/usr/bin/python
If you are using a script heavily on a unix-based maching (mac,linux) making an alias can be useful:
alias command="python path/to/script.py"

1 Comment

It might be better to suggest functions than aliases -- there's no practical difference in this specific case, but there are a great many situations where aliases aren't powerful enough to be applicable (anywhere some kind of logic needs to happen during expansion, or where arguments would go anywhere but the end, etc). mycommand() { python /path/to/myscript "$@"; }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.