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?
In general, there are at least five ways to run programs through the Python interpreter: Interactively. As Python module files. As Unix-style script files.
The most basic and easy way to run a Python script is by using the python command. You need to open a command line and type the word python followed by the path to your script file, like this: python first_script.py Hello World! Then you hit the ENTER button from the keyboard and that's it.
There is more than one way to run a python script but before going toward the different ways to run a python script, we first have to check whether a python interpreter is installed on the system or not. So in windows, open 'cmd' (Command Prompt) and type the following command.
The answer lies in a combination of two things:
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With