Importing python modules from folders -
so, having following structure in python package:
./main.py __init__.py ./mymods/ __init__.py a.py b.py
my module a.py imports module b:
import b
now, want import module main when following:
import mymods.a importerror: no module named 'b'
i googled couldn't find solution particular problem. samaritan knows how this?
p.s. prefer not have import module b explicitly main, if possible.
you need make mymods
package. can done creating empty __init__.py
file in directory.
➜ tree . ├── main.py └── mymods ├── __init__.py ├── a.py └── b.py 1 directory, 4 files ➜ cat main.py import mymods.a print 'printing main' ➜ cat mymods/a.py . import b print 'printing a' ➜ cat mymods/b.py print 'printing b ➜ python main.py printing b printing printing main
for python 3, change import b
from . import b
.
Comments
Post a Comment