0

All,

File structure:

\
util
  utils.py
modules
  __init__.py
  modules1.py
  submodule
     __init__.py
     submodule.py

I want to know how to import utils.py in those

__init__.py 

for example, now I run the python interpreter in \ level, I run import modules, I suppose the code from ..util.utils import * may works, but it is not.

may I know where is the mistake? and may I know if there's a way i can import utils.py in a universal format? something like

import \util\utils.py

I know I may use path.append(), but any alternative?

Thanks

============

got the answer from this post:

Import a module from a relative path

Community
  • 1
  • 1
user478514
  • 3,859
  • 10
  • 33
  • 42

1 Answers1

1

If you are developping a python package (what you are obviously doing, as you have init.py), then the most simple way to import your module is just via the package. For example, if your package is called mypackage, then:

import mypackage.utils
Pupkov-Zadnij
  • 1,342
  • 2
  • 11
  • 21
  • thanks, but may be not a package but an application if I add a C-like main.py on the \ folder. So is you means i can pack all the codes to a folder then add another __init__.py? and move the init involing outside of this folder? – user478514 Feb 18 '11 at 09:50