1

I have a Python project with the following file structure:

Under each Python file I have shown the lines of code that I am using to import:

.
├── README.md
├── requirements.txt
└── warbot
    ├── bots
    │   ├── __init__.py
    │   ├── telegram_bot.py
    │   │     │ from warbot.lib.admin import WarBotAdmin
    │   │     │ from warbot.lib.vars import TELEGRAM_VARS, DATABASE_FILENAME
    │   └── twitter_bot.py
    │         │ from warbot.lib.twitter import WarBotTwitter
    │         │ from warbot.lib.vars import TWITTER_VARS, DATABASE_FILENAME
    ├── database
    │   └── warbot_db.json (TinyDB database)
    ├── __init__.py
    ├── lib
    │   ├── admin.py
    │   │     │ from .telegram import TelegramInterface
    │   │     │ from .database import WarBotDB
    │   │     │ from .warbot import WarBot
    │   │     │ from .vars import log
    │   ├── api.py
    │   │     │ from .database import WarBotDB
    │   ├── database.py
    │   │     │ from .vars import log
    │   ├── imagehandler.py
    │   ├── __init__.py
    │   ├── telegram.py
    │   ├── twitter.py
    │   │     │ from .warbot import WarBot
    │   │     │ from .api import WarBotAPI
    │   │     │ from .database import WarBotDB
    │   │     │ from .imagehandler import WarBotImageHandler
    │   ├── vars.py
    │   └── warbot.py
    │         │ from .database import WarBotDB
    ├── __main__.py
    ├── output (some logs)
    │   ├── logs_telegram.txt
    │   └── logs_twitter.txt
    ├── resources
    │   ├── (various resources)
    └── tmp
        └── info.txt

I am trying to compile the telegram_bot.py and twitter_bot.py files, but their compilation result in a ModuleNotFoundError: No module named warbot.

Furthermore, when I try to compile, just to check the imports, the file admin.py (as an example), it throws me ModuleNotFoundError: No module named '__main__.telegram'; '__main__' is not a package.

I have been documenting myself about Python imports, and even my IDE does not lint errors at this structure.

mianfg
  • 25
  • 7
  • 3
    What do you mean you are trying to *compile*, which file are you running exactly? – WiseDev Jul 09 '19 at 10:59
  • @hiroprotagonist There is an `__init__.py` file, only that `tree` orders it alphabetically – mianfg Jul 09 '19 at 11:02
  • @BlueRineS when I say _compile_ (which is a bad term, sorry), I mean running `python filename` on that file. Saying "compiling `admin.py`" means doing `python admin.py` – mianfg Jul 09 '19 at 11:03
  • maybe try `from ..warbot` instead of `from warbot`? – WiseDev Jul 09 '19 at 11:06
  • @BlueRineS can you specify where? – mianfg Jul 09 '19 at 11:14
  • telegram_bot and twitter_bot? That's where you use that code – WiseDev Jul 09 '19 at 11:15
  • @BlueRineS Does not work either – mianfg Jul 09 '19 at 11:17
  • not even just `.warbot`? and does it still return the same error and point to the same line? – WiseDev Jul 09 '19 at 11:18
  • @BlueRineS I tried on the file `telegram_bot.py` **Option 1.** Replaced to: `from ..warbot import WarBotAdmin; from ..warbot import TELEGRAM_VARS, DATABASE_FILENAME`. Error: `ValueError: attempted relative import beyond top-level package`. **Option 2.** Replaced to: `from .warbot import WarBotAdmin; from .warbot import TELEGRAM_VARS, DATABASE_FILENAME`. Error: `ModuleNotFoundError: No module named '__main__.warbot'; '__main__' is not a package` – mianfg Jul 09 '19 at 11:31
  • Aah, you should look up the error `attempted relative import beyond top-level package`. Usually it is expected that your `main` file is in the same level as your `top-level` folder, i.e. no parent folders. – WiseDev Jul 09 '19 at 11:35
  • Take a look at [beyond top level package error in relative import](https://stackoverflow.com/questions/30669474/beyond-top-level-package-error-in-relative-import) – WiseDev Jul 09 '19 at 11:38
  • I'm sorry, but I cannot figure this out. Can you check out my project and see if you can find out? https://github.com/mianfg/warbot Thanks in advance, @BlueRineS – mianfg Jul 09 '19 at 12:24
  • maybe I will try tonight, am in office now :| – WiseDev Jul 09 '19 at 12:33

0 Answers0