Hyphen in django app

Well, I came across a problem with import and a Django application that has a hyphen in its name.

As far as I know, hyphens are not even allowed in Django application names, but they still exist and the site works without errors. I donโ€™t know at all how the application was created, since the project already existed when I started working on it. (This is a Django CMS project if it changes btw something)

Now the problem is that when I want to import something in the shell or in the project, I canโ€™t just write from my-app import module, because it always throws an error due to hypen.

Also INSTALLED_APPSlisted in it using hypenINSTALLED_APPS = ('my-app')

So how can I fix this and make it usable? I was thinking about renaming, but I really don't know the consequences, and I also do not want to break anything in CMS (I am still learning Django, and CMS is very new to me)

+4
source share
1 answer

You can do it:

import importlib
module = importlib.import_module('.module', 'my-app')

See the documentation https://docs.python.org/3/library/importlib.html#importlib.import_module for details

Edit: this is not a specific problem with Django, it means that in Python (and in most other programming languages) '-' (hyphen) cannot be used in variable names (As '-' is a subtraction operator).

+3
source

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


All Articles