0

I am newbie to programming. I am importing a function from a.py to b.py (i.e from pkg.subpkg1.a import func) but it give me an error (ModuleNotFoundError: No module named 'pkg')

directory structure:

root-dir
       /pkg
           /subpkg1
                a.py
           /subpkg2
                b.py
Abdullah
  • 87
  • 1
  • 4

1 Answers1

0

There is something called __init__.py that will mark directories on disk as Python package directories. so that you can access other py files Sample Dir Structure

a.py contains

print("I am a.py")

and b.py contains

import a # here you should mention parent directory
print("I am b.py")

running b.py will give you,

I am a.py
I am b.py
Dhamodharan
  • 199
  • 10
  • every package contain the __int__.py but still it doesn't work from CLI. it works fine in pycharm. @Dhamodharan – Abdullah Apr 26 '19 at 10:07
  • Since we are using systems default interpreter and the directory structure got changing. just change **bold**from pkg import subpkg1 **bold** to **bold**import subpkg1**bold** . then run subpkg2 , python will autometically create compiled version of subpkg1 & you may see "_pycache_" folder. – Dhamodharan Apr 26 '19 at 10:34
  • still didn't work for me – Abdullah Apr 26 '19 at 11:05
  • Please check this demo https://github.com/dhamodharanrk/examples/tree/master/PackageDemo – Dhamodharan Apr 26 '19 at 11:25