0

I am using cookiecutter-django on my project and having problem while trying to import settings from config outside app. Getting error ModuleNotFoundError: No module named 'config' Project structure

project
 ┣ .envs
 ┃ ┗ .local
 ┃ ┃ ┣ .bot
 ┃ ┃ ┣ .django
 ┣ bot
 ┃ ┣ __init__.py
 ┃ ┗ bot.py
 ┣ compose
 ┃ ┣ local
 ┃ ┃ ┣ django
 ┃ ┃ ┃ ┣ Dockerfile
 ┃ ┃ ┃ ┗ start
 ┃ ┃ ┗ pytelegrambot
 ┃ ┃ ┃ ┣ Dockerfile
 ┃ ┃ ┃ ┗ start
 ┣ config
 ┃ ┣ settings
 ┃ ┃ ┣ __init__.py
 ┃ ┃ ┣ base.py
 ┃ ┃ ┣ local.py
 ┃ ┣ __init__.py
 ┃ ┣ urls.py
 ┃ ┗ wsgi.py
 ┣ project
 ┃ ┣ app
 ┃ ┃ ┣ migrations
 ┃ ┃ ┃ ┗ __init__.py
 ┃ ┃ ┣ admin.py
 ┃ ┃ ┣ apps.py
 ┃ ┃ ┣ signals.py
 ┃ ┃ ┣ models.py
 ┃ ┃ ┗ views.py
 ┣ requirements
 ┃ ┣ base.txt
 ┣ README.md
 ┣ local.yml
 ┣ manage.py

bot.py

import telebot
from config.settings.base import env

bot = telebot.TeleBot(env('BOT_TOKEN'))

def send_welcome(message):
    print(message)

if __name__ == '__main__':
    bot.infinity_polling()

signals.py

from bot.bot import send_welcome

@receiver(post_save, sender=Model)
def translate(sender, instance, created, **kwargs):
    send_wlcome("Hi")

Here I am sending message on telegram bot when object created. If I try to use os.environ['BOT_TOKEN'] it gives me another error from signals.py KeyError: 'BOT_TOKEN'. BOT_TOKEN is located in .envs/.local/.bot Please, can anyone help ?

mirodil
  • 422
  • 2
  • 8
  • 21

1 Answers1

0

You have to set your code in project directory.

Django cookiecutter defines the root path project to Project directory, so all code have to be set there.

In manage.py file, you can see the code which make the project directory as root path. You can change it, but i do not sure it is a good idea.

Lucas Grugru
  • 1,664
  • 1
  • 4
  • 18
  • Can you clarify your answer please? tried `sys.path.append(str('path/to/code'))` but didn't help – mirodil Jan 09 '23 at 10:36
  • you do not understand. bot.py is a independant code, so, you have to add path to other lib inside directly. So you add the path or you import in relative way: `from ..config.settings.base import env`. – Lucas Grugru Jan 09 '23 at 14:02