-3

i have a folder with a bot and a folder with a future api. The problem is that when I try to import a class from the database folder in the bot, it gives an error that this package does not exist. inits are all written, what could be wrong? Or how to fix it?

In the bot folder in the init, I do the import - from .database import GetCategories

Next in the api folder (top) I try to import this class into the main.py file - from bot import GetCategories

But I get an error - ModuleNotFoundError: No module named 'bot'

Example:

folder api:

main.py code:

from fastapi import FastAPI
from bot import GetCategories
app = FastAPI()

@app.get("/get_categories/{auth_token}")
def read_item(auth_token: str):
    i = GetCategories()
    return 'test' 

folder bot:

init code: from .database import GetCategories

folder database:

init code: from .categories import GetCategories

categories.py code:

class GetCategories:

    print('done')

run main.py in folder api: uvicorn main:app --reload

example structure

1 Answers1

0

Try:

from bot.database import GetCategories

But first you need to take main.py out right in the folder where bot directory is OR move bot directory to where main.py is, other wise you will need to make package of this database directory then import it.

  • I made a mistake, this option does not suit me, how to make a package of this directory? I never did it. – Dervin Sangalang Oct 12 '22 at 17:20
  • https://betterscientificsoftware.github.io/python-for-hpc/tutorials/python-pypi-packaging/#creating-a-python-package This article will help you, just take some time and do it. It wil help you in future for sure. If complicated, start with dummy python files and folders. FYI make it of database folder, then import. – Sarim Bin Waseem Oct 12 '22 at 17:23