0

I am having issues with importing, my own, pip package named the same as a module inside a django-app. Lets say my own pip module is called: fancymodule. I then have a module-folder inside a django-app named the same:

* django-project
    * payments
        * models.py
        * fancymodule/
            * __init__.py

The problem I am having, is that inside payments/models.py, I import:

from fancymodule import ApiClient

This is suppose to reference the fancymodule installed through pip. But is instead referencing the module inside the payments-django-app instead.

If I wanted to reference the module inside the payments-django-app, in my head, this should be:

from payments.fancymodule import whatever

I get that from the view of payments/models.py, the import fancymodule .. will reference fancymodule inside the payments-folder.. but can i change/fix this, so it reference the fancymodule installed through pip ?

FYI: Working on an old legacy project.

Home someone can help.

Thor A. Pedersen
  • 1,122
  • 4
  • 18
  • 32
  • 2
    Why does your PYTHONPATH have the path to the django (django-project/payments) app in it? Remove that and you're golden. –  Nov 09 '20 at 09:36
  • are you using VS code? if so there should be a settings.json file in a .vscode folder where you can modify that easily. – Chris Nov 09 '20 at 09:50
  • hmmm Melvyn, that is a good question.. its a old project i didn't start, so might be the place i need to look into.. ill come back with what i find :) I am using pycharm, so no vscode stuff. – Thor A. Pedersen Nov 09 '20 at 10:03
  • 3
    For Python 2.x importing `fancymodule` from `payments/models.py` would import `payments.fancymodule`. To avoid that you should add `from __future__ import absolute_import` (see PEP328, py2.5+) at the top of the `models.py` (beware that other imports may fail). For Python 3.x the issue may be related to PYTHONPATH. – marcinn Nov 09 '20 at 10:07
  • Does this answer your question? [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) – anthony sottile Nov 10 '20 at 08:35

1 Answers1

0

marcinn suggestion about using from __future__ import absolute_import worked.

So my solution inside the models.py file ended up being:

from __future__ import absolute_import
from fancymodule import ApiClient

And as I said to above comment.. I couldn't find anything wrong with my PYTHONPATH. as marcinn also said, this is most likly a Python 3.x thing.. and since i am on 2.7, it makes sense the PYTHONPATH looks fine.

OBS: PyCharm redlines the from fancymodule import ApiClient, but it works. So i think its just PyCharm that needs to be restartet to remove the redlines.

Thor A. Pedersen
  • 1,122
  • 4
  • 18
  • 32