4

I am working on a django project that has uses multiple applications (python modules). Most of those python modules are maintained by other people in their own git repositories. I use the git-submodules command to import them into my project under the 'apps' directory like so:

mysite/
mysite/apps
mysite/apps/django-extensions
mysite/apps/django-celery
mysite/apps/django-comments
mysite/apps/myapp
...etc

Most of those submodules (take django-extensions for example) have a subfolder containing the actual python module: mysite/apps/django-extensions/django_extensions

This means I can't simply set my python path to include mysite/apps--I have to set it to include mysite/apps/django-extensions so it can import the django_extensions subfolder.

It gets annoying typing:

PYTHONPATH=mysite/apps/django-extensions:mysite/apps/django-celery... python manage.py runserver

Is there an easier way I should be laying out my repo? An easier process? Just for fun, I tried a PYTHONPATH of mysite/apps/*, but that didn't work.

Aaron C. de Bruyn
  • 2,347
  • 1
  • 30
  • 40

4 Answers4

9

This is the wrong way to do it. Don't install other people's third-party code in your own project file. Instead, create a virtualenv, and install the code directly using pip.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 2
    What's wrong with using git submodules to check out all the components of a django project? It's much easier for me to follow upstream, see breaking changes, 'pin' to a specific release, etc... There doesn't seem to be an advantage to me to use git for managing my sources and then learning virtualenv and pip to manage everyone elses... Plus it's a lot easier for testers to do to a 'git pull; git submodule update --init --recursive'. – Aaron C. de Bruyn Mar 01 '11 at 02:38
  • I agree, and have been frustrated by this in the past. There is a git extension that lets you map directors from remote repositories (git subtree), but its buggy and difficult to maintain. What Daniel suggests seems to be the 'standard' approach, although for the reasons you mention above it seems like just a much of a hack as the (decent) solution you came up with. I'll be trying you approach. – Nathan Keller Aug 13 '12 at 13:29
5

After coming up blank on the internet, I hacked this solution together. It's straight forward and works well enough:

#At the top of settings.py
import sys, os
git_sub_modules = '/path/to/dir/containing/submodules' #Relative paths ok too
for dir in os.listdir(git_sub_modules):
    path = os.path.join(git_sub_modules, dir)
    if not path in sys.path:
        sys.path.append(path)

time passes

UPDATE: It's much easier to use a virtualenv and/or something like dokku for deploying apps. I no longer use this. Although it is still a pain to checkout 3rd party apps that need 'tweaks' and use them in the project.

Aaron C. de Bruyn
  • 2,347
  • 1
  • 30
  • 40
1

You could tuck those paths in a dependencies.pth file, and only have the .pth in your path. There are examples in your site-packages / dist-packages.

Tobu
  • 24,771
  • 4
  • 91
  • 98
0

Could you try checking out just the wanted part of the repository? So if they have the actual code inside of what you're checking out, don't check out the extra part.

So instead of getting django-extensions get django-extensions/django-extensions.

Edit: I believe this is what you could do above.

Also, I believe you could get away with adding an __init__.py in the first django-extensions directory, but then you're going to have to add an extra django-extensions to your imports as well (__init__.py tells python it is a package). While I think this might work, I would recommend shooting for my first example.

Community
  • 1
  • 1
wilbbe01
  • 1,931
  • 1
  • 24
  • 38
  • While git supports sparse checkouts now in version 1.7, it does nothing to affect the path. I think I might just need to add something to my __init__.py that scans through mysite/apps/* and adds the directories to the python path. – Aaron C. de Bruyn Feb 28 '11 at 00:56