Maritn Ge
Maritn Ge

Reputation: 1226

Python ModuleNotFound in file when it is called from other directory, but not when it is directly executed

So bascically I have a filestructure lile

directory
/subdirecrory
  >view.py
  >view_model.py
  >controller.py
>main.py

My controller looks something like:

import view

startChat()
#^ for testing, to see if the import works when directly calling file

def startChat(socket):
    #datahandling
    view.startGui()

My view is simply:

import tkinter

def startGui():
    gui = tkinter.Tk()
    gui.mainloop()

And lastly, the main is:

from subdirectory import controller

if __name__ == '__main__':
    controller.startChat(s)

I removed all the meat to reduce myself to the GUI starting. When I run the controller everything works as it should, if I put the main into the subdirectory it also works.

Important note: This is a fix, but not what I want. I will soon again need different files, folders and directories and unless there is no way to do this would like to know a solution to this problem that doesn't involve putting everything in the same folder.

If I run the program as it is right now, it will execute the controller.py, if i put a print() above the imports (of which i have a few, like sys and time, which all work), it will only fail once it reaches the import view

The errormessage is a:

Exception has occurred: ModuleNotFoundError
No module named 'chat_view

My theory is that when calling from another directory the runtime has no info about the folder it is put into, and can't do what would happen if I started it from the directory. Which is what I tried to fix in the first and third solution of the other things I have tried so far:

As you might see I am more trying around and guessing and I think it's unlikely I find the solution to this anytime soon, so I thought to ask here. I wouldn't be surprised if this is a duplicate, but I wasn't able to find what I was looking for.

Upvotes: 0

Views: 51

Answers (2)

Maritn Ge
Maritn Ge

Reputation: 1226

A weird thing worked that I hope someone else can EXPLAIN, but here for future reference what I did was:

I put all the files into a directory names "src" like

directory/src #this is how it is displayed in VSC, idk why it is not displayed as extra directory but like this?
  main.py
  /subdirectory
    __init__.py
    #other files

I did this to clean up my code but then in the controller the import import view didn't work anymore, so when I changed it to from subdirectory import view (which earlier caused an error) it now works calling it from the main.py

This is bizarre to me as all I did was add a directory, but it works, so, yes. This is the accepted answer for now, until someone explains it better than me then I will switch it

Upvotes: 0

Ad115
Ad115

Reputation: 26

Instead of an absolute import, you can use a relative import in your controller

from . import view

the dot means it will search in the same folder.

Upvotes: 1

Related Questions