Running Django commands from anywhere

06 May · by Tim Kamanin · 2 min read

One of the most annoying aspects of developing in Django for me is running commands: you need to be in the same directory with file in order to run a command. This is not convenient. I thought it would be cool to be able to run from anywhere in the project as much as you can run git command from anywhere within a git project.

The solution is simple: create a bash script that accepts arguments, takes your current directory and searches for file in that directory recursivelly and then if file not found there, find goes up a directory and up and up until it finds nearest or hits the root ('/'). When it finds it executes it with arguments passed to the bash script. Simple idea, let's implement:


while [[ "`pwd`" != '/' ]]; do
  managepy_path=$(find $(pwd) -type f -name "" -print | head -n 1)
  if [[ -n $managepy_path ]]; then
echo "Running 'python $managepy_path $@' command:"
python $managepy_path $@
cd ..

if [[ managepy_path == 0 ]]; then
  echo 'Cant find'

Save it to your local bin: /usr/local/bin as (yes, this is not python file, but it has .py extension to make it sound as vanilla Django file, you can name it anything you want actually).

And then run it from somewhere deep in your Django project:

python --help

And you'll get the output of the nearest command executed within your currently activated virtualenv or default python environment.

Git repo

I added the script to its very own git repo at and you can now use pip to install the package:

pip install django_managepy_anywhere

Enjoy and let me know how it works for you :)

Want to get more 🔥 tips like this one?

Subscribe to get notified about new dev tutorials