user478514
user478514

Reputation: 4059

how to import py file from different upper folder?

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

Upvotes: 0

Views: 2698

Answers (1)

Pupkov-Zadnij
Pupkov-Zadnij

Reputation: 1392

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

Upvotes: 1

Related Questions