1

I am using modified sources from few third party apps in my project. I would like to put these third party apps in a separate directory, so that they are not on the same directory level as my own apps. Is this possible in django?

I tried simply putting the apps in a directory thirdparty and changed my INSTALLED_APPS like so:

INSTALLED_APPS = (
    'my_app',
    ...
    'thirdparty.django_messages',

This of course failse with:

ImportError: No module named thirdparty

After which I naturally added __init__.py to the directory. But it fails again:

ImportError: No module named django_messages.apps

Just to avoid any confusion, the app django_messages does contain apps.py

Is there a way to group django apps in a directory or do they all have to be in the same project root directory?

Edit

A better alternative is in the accepted answer by Antoine Pinsard

For those persistent on grouping apps see accepted answer here!

Community
  • 1
  • 1
dsalaj
  • 2,857
  • 4
  • 34
  • 43
  • Its unclear what you're trying to achieve. [What is the XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Sayse Mar 11 '16 at 12:20
  • @Sayse what is unclear? I ask in the title and in last sentence precisely what I am trying to achieve. I will attempt to make the question clearer. – dsalaj Mar 11 '16 at 12:26
  • 1
    What is unclear is *why* you want to do this, what problem are you trying to fix by doing so? – Sayse Mar 11 '16 at 12:27
  • @Sayse Just organizing a project which contains a lot of apps. Wanted to check if this is possible. – dsalaj Mar 11 '16 at 12:29
  • But you're not achieving anything by doing it, if you really want to, add a comment line above the third party apps – Sayse Mar 11 '16 at 12:30

1 Answers1

5

Don't do this. If you really need to modify the source code of third-party apps, fork the repositories so that you will be able to watch and merge upstream updates.

Then install the modified apps with pip.

For instance, if you have forked django-autocomplete-light on your github (let's say https://github.com/dsalaj/django-autocomplete-light):

pip install git+ssh://git@github.com/dsalaj/django-autocomplete-light.git

You will be able to upgrade it like any other pip package:

pip install --upgrade git+ssh://git@github.com/dsalaj/django-autocomplete-light.git

And even add it to your requirements.txt.


As Mad Wombat mentioned in the comments, you can use pip's --editable (-e) option to install these packages in a specific folder within your project. From pip help:

-e, --editable Install a project in editable mode (i.e. setuptools "develop mode") from a local project path or a VCS url.


Nevertheless, to answer the question. The issue is that the app django_messages considers it is a top-level module (and it is supposed to be). Thus it can import its submodules using an absolute python path (starting with django_messages.). However, when you place it within a module thirdparty, django_messages becomes a submodule of thirdparty. You could add the thirdparty directory to your PYTHON_PATH so that django_messages is available as a top-level module. But it is really not advisable to do so. lib/pythonX.Y/site-packages is the best place for your third party packages and this is where pip installs them.

You may also be interested in python virtualenvs if you don't know what they are.

Antoine Pinsard
  • 33,148
  • 8
  • 67
  • 87
  • Thank you for the advice, it is a good alternative. But could you please answer if it is possible to do what I'm trying to achieve. – dsalaj Mar 11 '16 at 12:29
  • 3
    You really don't want to achieve what you are trying to. – Antoine Pinsard Mar 11 '16 at 12:30
  • I would be thankful for an elaboration on why this is such a bad idea. Anyway [here](http://stackoverflow.com/questions/2862084/how-do-i-create-sub-applications-in-django) is the possible answer to what I was looking. – dsalaj Mar 11 '16 at 12:48
  • Very much appreciated. – dsalaj Mar 11 '16 at 13:04
  • 1
    Agree with all that Antoine said. But there are actually ways to keep your customized libraries separate from regular pip packages. You can use pip "editable" installs `pip install -e git+ssh://github.com/whatever`. This will install packages into ./src directory in your current dir and will update a .pth file in python site-packages to include it. The other way is to create a custom .pth file and place it manually in site-packages. Both ways have their advantages and disadvantages, too long to list in a comment. – Mad Wombat Mar 11 '16 at 16:21