Javaaaa
Javaaaa

Reputation: 3804

How can I execute a python script directly (without prefixing by the python command) from bash?

I am just starting to use terminal for my programming needs. In a lot of Django tutorials I see people say, for example, I should type this in terminal:

manage.py runserver

However when I do this it says:

bash: manage.py: command not found

I get it to work when I do: python manage.py runserver, however I would like to understand why this works and the other method doesn't. I guess these are some very basic things but I thought I'd ask here.

Upvotes: 3

Views: 5786

Answers (4)

MindTooth
MindTooth

Reputation: 5150

I've cooked together a small "script" to automate this: (just copy whole text, and paste inside your active terminal.)

tee -a ~/.profile <<EOF

if [ -d "/Library/Python/2.6/site-packages/django/bin" ] ; then
    PATH=/Library/Python/2.6/site-packages/django/bin:$PATH
fi
EOF

Doesn't django-admin.py do the same? I think so, because I can find manage.py inside my ../bin folder. And stated at the official documentation, they do the same. So I believe ;)

Also, have you obtained Django via easy_install? My script expect that you are using Snow Leopard with the system version (Python 2.6).

Upvotes: 0

anubhava
anubhava

Reputation: 785058

It is because your manage.py is not an executable script.

First put this line at the top of manage.py (assuming your python is in /usr/bin/python):

#!/usr/bin/python

Then make your script executable:

chmod +x manage.py

Then try to execute your script ./manage.py runserver.

Read this link for more info: http://effbot.org/pyfaq/how-do-i-make-a-python-script-executable-on-unix.htm

Upvotes: 12

sarnold
sarnold

Reputation: 104050

bash(1) will search your PATH environment variable to find programs to execute. PATH does not normally contain your "current working directory" (.) because that opens people up to trivial security problems:

cd /home/unsavory_character/
ls

If unsavory_character places an executable in /home/unsavory_character/ls that adds his or her ssh(1) key to your ~/.ssh/authorized_keys file, you'd be in for a surprise -- he or she could log in as you without a password.

So systems these days don't add the current working directory to the PATH, because it is too unsafe.

The workaround:

./manage.py runserver

Of course, that assumes your current working directory is whichever directory contains the manage.py script. That might be a safe assumption. If you'd like to be able to execute it from anywhere in the filesystem, you can add the directory to your PATH by editing your ~/.profile or ~/.bash_profile or ~/.bashrc file. (If one of them already exists, pick that one. I seem to recall others with PATH problems on OS X found one or the the other file worked well, and the other one never got executed.)

(In my case, I have a bunch of self-written utilities in ~/bin/, but yours might be elsewhere. Change the paths as appropriate.)

if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

Upvotes: 4

Ben
Ben

Reputation: 6967

manage.py needs to be executable. Try: chmod +x manage.py

Upvotes: 0

Related Questions