-1

I want to import function from bot/main.py into bybitapi/server.py. The structure of project looks like this:

enter image description here

So, in server.py I was trying to do it like this:

from bot.main import notifyuserwithtriggeredalarms

But it doesn't work, it was about bot package doesn't exists. Also, I was trying to solve this using os and sys, I couldn't, so, how can I import function in server.py though other folder?

dokichan
  • 857
  • 2
  • 11
  • 44
  • Does this answer your question? [Importing files from different folder](https://stackoverflow.com/questions/4383571/importing-files-from-different-folder) – medium-dimensional May 08 '22 at 12:23
  • I saw this, actually, it doesn't. – dokichan May 08 '22 at 12:26
  • Pretty sure it does answer your question. Do `sys.path.append('/path/to/project/directory')` and your imports will work. (That said, I don't think messing with `sys.path` is a nice and clean solution.) – Aran-Fey May 08 '22 at 12:42

1 Answers1

-2

mport a Module From the Parent Directory in Python by Adding It to PYTHONPATH

import os, sys

p = os.path.abspath('.')
sys.path.insert(1, p)

from main import notifyuserwithtriggeredalarms
Anytokin
  • 117
  • 5
  • This doesn't really do anything. You're adding the current working directory to `sys.path`, but 99% of the time (it depends on how you start python) it's already there. And even if it isn't, the CWD might not necessarily be set to the directory you need. Use something like `pathlib.Path(__file__).parent` to find the parent directory instead. – Aran-Fey May 08 '22 at 12:38