TurboC
TurboC

Reputation: 908

ModuleNotFoundError: I can't import custom modules properly

below the folder structure of my software:

enter image description here

below the code of all the .py files:

run.py:

import modules.module_01.aa as a

a.test()

# test:
if __name__=="__main__":
    pass

aa.py (module 1):

import libraries.qq as q
import libraries.zz as z

def test():
    q.qq_fun()
    z.zz_fun()
    print("ciao")

qq.py (library used by aa.py):

def qq_fun():
    pass

zz.py (library used by aa.py):

def zz_fun():
    pass

my question is really simple, why when I run "run.py" Python say to me:

enter image description here

why "aa.py" can't import the module "qq.py" and "zz.py"? how can I fix this issue?

Upvotes: 0

Views: 1720

Answers (1)

Rain
Rain

Reputation: 26

run.py

In run.py, the Python interpreter thinks you're trying to import module_01.aa from a module named module. To import aa.py, you'll need to add this code to the top of your file, which adds the directory aa.py is in to the system path, and change your import statement to import aa as a.

import sys

sys.path.insert(0, "./modules/module_01/")

aa.py

The same problem occurs in aa.py. To fix the problem in this file, you'll need to add this code to the top of aa.py, which adds the directory qq.py and zz.py are in, and remove the libraries. from both of your import statements.

import sys

sys.path.insert(0, "./modules/module_01/libraries")

Upvotes: 1

Related Questions