Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a list of python packages used by a Django Project

Tags:

python

django

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.

like image 725
Daniel Nill Avatar asked Nov 17 '11 16:11

Daniel Nill


People also ask

How do I get a list of Python packages?

The Pip, Pipenv, Anaconda Navigator, and Conda Package Managers can all be used to list installed Python packages. You can also use the ActiveState Platform's command line interface (CLI), the State Tool to list all installed packages using a simple “state packages” command.

How do I get the dependencies for a project in Python?

Download Dependencies OnlyUse the pipdeptree utility to gather a list of all dependencies, create a requirements. txt file listing all the dependencies, and then download them with the pip download command. Get the list of dependencies for a package from the setup.py file.

How do I list pip packages?

If you want to list all the Python packages installed in an environment, pip list command is what you are looking for. The command will return all the packages installed, along with their specific version and location. If a package is installed from a remote host (for example PyPI or Nexus) the location will be empty.

How do I see all dependencies in Python?

Instead, package dependencies can be seen using one of the following commands in Python: pip show: List dependencies of Python packages that have already been installed. pipdeptree: List the dependencies in a tree form. Pip list: List installed packages with various conditions.


1 Answers

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.

like image 96
Shawn Chin Avatar answered Oct 05 '22 23:10

Shawn Chin