17

Is there any easy way to get a list of python packages used by a Django project?

I've looked at snakefood and this question, but neither seem to play nicely within the django environment.

Ideally I'm looking for a command I can execute from the python shell or from bash to list all of the pypy supported dependencies that aren't native to django.

Community
  • 1
  • 1
Daniel Nill
  • 5,539
  • 10
  • 45
  • 63
  • grep import -r /path/to/your/django/project – Tom O'Connor Nov 17 '11 at 16:53
  • @TomO'Connor I had tried grep but couldn't figure out how to get it refined enough so that it was just printing out my import statements. And even then it would still be a pain to sort through all that and figure out which are the actual non-native packages – Daniel Nill Nov 17 '11 at 17:01
  • 1
    `grep import ...` is not enough. How about those imported using `from x import y`? Then there's also apps loaded from settings.INSTALLED_APPS (and their dependencies) which may be external to a django project. – Shawn Chin Nov 17 '11 at 17:54

4 Answers4

7

This isn't a complete answer but hopefully it'll make a sensible starting point.

From what I can tell, the dependencies of a django project (apart from django itself and its dependencies*) consists of:

  1. Modules imported by your django project
  2. Apps loaded by your project via settings.INSTALLED_APPS (and their dependencies)

#1 Modules imported by your project

You can probably discover this using snakefood.

#2 Apps loaded via settings.INSTALLED_APPS

Running the following script should give the path to apps listed in INSTALLED_APPS:

#!/usr/bin/env python
from settings import INSTALLED_APPS
from django.utils.importlib import import_module
import os

app_names = (x for x in INSTALLED_APPS if not x.startswith('django'))
app_paths = (os.path.dirname(os.path.abspath(import_module(x).__file__)) for x in app_names)    
print "\n".join(x for x in app_paths if not x.startswith(os.getcwd()))

You can then pass this on to snakefood to discover their dependencies.


* To be thorough, it should be possible to discover the various backends (db/cache/auth/etc.) from settings and include the associated modules into your list of dependencies.

Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
6

Maybe this is useful, in case the project used 'pip' to install the dependencies/libraries:

pip freeze
stackex
  • 643
  • 10
  • 11
  • 8
    This will just show you what's installed, not what's being *used*. Particularly problematic if you're using an environment shared between multiple projects. – Oli Apr 02 '15 at 08:08
3

run this command inside your django project, this will write all the installed packages inside requirements.txt file

 pip freeze > requirements.txt

or if you are using python3 then use pip3 like this

pip3 freeze > requirements.txt

you will find the requirements.txt file inside your django project

Shahid Ghafoor
  • 829
  • 8
  • 17
  • You're assuming that a virtualenv is being used. If that's not the case, this will write all system installed packages, which may not be what the user wants. – Joel G Mathew May 30 '21 at 13:06
2

pigar works fine for me.

It doesn't cover INSTALLED_APPS yet, but that's just a little extra work.

velis
  • 8,747
  • 4
  • 44
  • 64