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

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -