Django, Python Modules and Git Submodules

I am working on a django project that uses multiple applications (python modules). Most of these python modules are supported by other people in their git repositories. I use the git -submodules command to import them into my project in the apps directory, for example:

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

Most of these submodules (e.g. for django-extensions) have a subfolder containing the actual python module: mysite / apps / django-extensions / django_extensions

This means that I cannot just set my path in python to enable mysite / apps - I have to install it to enable mysite / apps / django-extensions so that it can import the django_extensions subfolder.

This is annoying typing:

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

, ? ? PYTHONPATH mysite/apps/*, .

+3
4

. . virtualenv pip.

+8

, , . :

#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)

UPDATE: virtualenv / - dokku . . - , "" .

+5

dependencies.pth .pth . site-packages/dist-packages .

+1

? , , , .

, django-extensions django-extensions/django-extensions.

: , , .

In addition, I believe that you can get away with adding __init__.py in the first django-extensions directory, but then you will also have to add additional django extensions for your import (__init__.py tells python this is a package). Although I think this might work, I would recommend shooting for my first example.

0
source

Source: https://habr.com/ru/post/1795122/


All Articles