1

Started learning Python recently and had a problem importing a model into django. I am trying to import a product model into Telegram bot handler but an error occurs.

Below is how my directory structure looks like: structure.png

Code:

from jangoMiniShop.products.models import Product

Error:

ModuleNotFoundError: No module named 'jangoMiniShop'

Code:

from ..products.models import Product

Error:

ImportError: attempted relative import with no known parent package
Horasachy
  • 33
  • 4
  • 1
    Does this answer your question? [ImportError : Attempted relative import with no known parent package](https://stackoverflow.com/questions/60593604/importerror-attempted-relative-import-with-no-known-parent-package) – Karthik Sep 02 '20 at 09:06
  • Do you have `__init__.py` file in `jangoMiniShop`? You can't use relative imports within the file you are executing (I presume you just ran `bot.py` or whatever you were running) – Kacperito Sep 02 '20 at 09:53
  • Yes, I had __init__.py, but it didn't work for me – Horasachy Sep 02 '20 at 10:02

1 Answers1

1

Found another solution, created a command that launches the bot, a new directory appeared in the telegram folder:

|management/
|---- commands/
|-------- __init__.py
|-------- bot.py
|---- __init__.py
| __init__.py

Import started working in bot.py file:

from products.models import Product

P.S. The question remained open. Why from products.models import Product didn't work in the bot.py file in the root of the project created with the command python manage.py startapp

Horasachy
  • 33
  • 4