Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

django-serializable-model name can't be imported due to hyphens #2

Closed
dev0088 opened this issue Aug 7, 2019 · 2 comments · Fixed by #3
Closed

django-serializable-model name can't be imported due to hyphens #2

dev0088 opened this issue Aug 7, 2019 · 2 comments · Fixed by #3

Comments

@dev0088
Copy link

dev0088 commented Aug 7, 2019

I added this code In settings.py file after run pip install django-serializable-model:

settings.py

    INSTALLED_APPS = (
        # ...
        'django-serializable-model',
    )

But I had this error:

ModuleNotFoundError: No module named 'django-serializable-model'

How can I use this package in my Django app?
I think your package name for importing must not include - character.

@agilgur5
Copy link
Owner

agilgur5 commented Aug 7, 2019

Hi @ninjadev999, thanks for reporting this issue :)

There's two different problems here, so let me address them separately:

  1. I'm not sure why you're using INSTALLED_APPS as that isn't how this package is used. Not sure if that's just what you were trying after from django-serializable-model import SerializableModel failed or if that's how you tried to initially use it.
    The installation and usage directions don't mention INSTALLED_APPS at all as this isn't a Django "application", it's just an ordinary Python module and is just imported into your code like most Python modules.

  2. The second part is totally my mistake. This is actually very confusing in Python-land, because one does not import a package by its PyPI name, dashes/hyphens are not encouraged, but also not illegal, despite causing syntax errors in normal usage, and even make up greater than 25% of PyPI packages. And then we have the conundrum that underscores, hyphens, and periods are actually treated equivalently by setuptools. Suffice to say, Python has not made this easy or intuitive to avoid, and perhaps made it easy to run into instead 😕

But addressing the question/problem directly, the actual module name of this package is currently serializable, so that means it's imported as:

from serializable import SerializableModel

and not as django-serializable-model which would cause a syntax error.
I think I noticed this at some point when publishing, but then forgot about it and then repeatedly looked at the README like "something feels wrong here..." 😅 . This was my first Python package, so my apologies for falling into Python's trap of extremely confusing package naming/importing.

You can use the workaround above for now to actually use the module. I'll create a wrapper or mirror module named django_serializable_model.py, add that to the py_modules config so it can be imported as django_serializable_model but without breaking/removing the current serializable until a major upgrade, and change the README to use underscores instead of hyphens in a patch release.

agilgur5 added a commit that referenced this issue Aug 8, 2019
- see #2
- in Python-land, packages are _not_ imported by their package name,
  but their _module_ name
  - see also https://stackoverflow.com/a/54599368/3431180
  - and the module name of this package was `serializable`, _not_
    `django-serializable-model`
- also in Python-land, one cannot use the standard `import` syntax
  for hyphenated modules, it will actually cause a SyntaxError
  - but for some reason it's not illegal to do so, and is allowed both
    in `setuptools` naming and PyPI naming (it is discouraged by PEP8)

- rename serializable.py to django_serializable_model.py
  - leave serializable.py and just make it a wrapper / alias of
    django_serialiazable_model so as not to cause a breaking change

(pkg): publish `django_serializable_model` module as well
- don't remove `serializable` though so as to not cause a breaking
  change; should remove it in v1.0.0

(docs): use `django_serializable_model` in Usage docs
agilgur5 added a commit that referenced this issue Aug 8, 2019
- see #2
- in Python-land, packages are _not_ imported by their package name,
  but their _module_ name
  - see also https://stackoverflow.com/a/54599368/3431180
  - and the module name of this package was `serializable`, _not_
    `django-serializable-model`
- also in Python-land, one cannot use the standard `import` syntax
  for hyphenated modules, it will actually cause a SyntaxError
  - but for some reason it's not illegal to do so, and is allowed both
    in `setuptools` naming and PyPI naming (it is discouraged by PEP8)

- rename serializable.py to django_serializable_model.py
  - leave serializable.py and just make it a wrapper / alias of
    django_serialiazable_model so as not to cause a breaking change

(pkg): publish `django_serializable_model` module as well
- don't remove `serializable` though so as to not cause a breaking
  change; should remove it in v1.0.0

(docs): use `django_serializable_model` in Usage docs
agilgur5 added a commit that referenced this issue Aug 8, 2019
- see #2
- in Python-land, packages are _not_ imported by their package name,
  but their _module_ name
  - see also https://stackoverflow.com/a/54599368/3431180
  - and the module name of this package was `serializable`, _not_
    `django-serializable-model`
- also in Python-land, one cannot use the standard `import` syntax
  for hyphenated modules, it will actually cause a SyntaxError
  - but for some reason it's not illegal to do so, and is allowed both
    in `setuptools` naming and PyPI naming (it is discouraged by PEP8)

- rename serializable.py to django_serializable_model.py
  - leave serializable.py and just make it a wrapper / alias of
    django_serialiazable_model so as not to cause a breaking change

(pkg): publish `django_serializable_model` module as well
- don't remove `serializable` though so as to not cause a breaking
  change; should remove it in v1.0.0

(docs): use `django_serializable_model` in Usage docs
@agilgur5
Copy link
Owner

agilgur5 commented Aug 8, 2019

@ninjadev999 the module naming issue has been resolved in v0.0.4. If you re-install with v0.0.4, you should be able to import the module as django_serializable_model:

from django_serializable_model import SerializableModel

Also sorry if you saw a stray comment here, that was meant for the PR, not this issue, it was moved there.

@agilgur5 agilgur5 changed the title django-serializable-model name not working in Django 1.11 django-serializable-model name not can't be imported due to hyphens Aug 8, 2019
@agilgur5 agilgur5 changed the title django-serializable-model name not can't be imported due to hyphens django-serializable-model name can't be imported due to hyphens Aug 8, 2019
agilgur5 added a commit to agilgur5/django-api-decorators that referenced this issue Aug 22, 2019
- see agilgur5/django-serializable-model#2
  - it had the same issue and its changes were cherry-picked here
- in Python-land, packages are _not_ imported by their package name,
  but their _module_ name
  - see also https://stackoverflow.com/a/54599368/3431180
  - and the module name of this package was `decorators`, _not_
    `django-api-decorators`
- also in Python-land, one cannot use the standard `import` syntax
  for hyphenated modules, it will actually cause a SyntaxError
  - but for some reason it's not illegal to do so, and is allowed both
    in `setuptools` naming and PyPI naming (it is discouraged by PEP8)

- rename decorators.py to django_api_decorators.py
  - leave decorators.py and just make it a wrapper / alias of
    django_serialiazable_model so as not to cause a breaking change

(pkg): publish `django_api_decorators` module as well
- don't remove `decorators` though so as to not cause a breaking
  change; should remove it in v1.0.0

(docs): use `django_api_decorators` in Usage docs
agilgur5 added a commit to agilgur5/django-api-decorators that referenced this issue Aug 22, 2019
- see agilgur5/django-serializable-model#2
  - it had the same issue and its changes were cherry-picked here
- in Python-land, packages are _not_ imported by their package name,
  but their _module_ name
  - see also https://stackoverflow.com/a/54599368/3431180
  - and the module name of this package was `decorators`, _not_
    `django-api-decorators`
- also in Python-land, one cannot use the standard `import` syntax
  for hyphenated modules, it will actually cause a SyntaxError
  - but for some reason it's not illegal to do so, and is allowed both
    in `setuptools` naming and PyPI naming (it is discouraged by PEP8)

- rename decorators.py to django_api_decorators.py
  - leave decorators.py and just make it a wrapper / alias of
    django_serialiazable_model so as not to cause a breaking change

(pkg): publish `django_api_decorators` module as well
- don't remove `decorators` though so as to not cause a breaking
  change; should remove it in v1.0.0

(docs): use `django_api_decorators` in Usage docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants