neversaint
neversaint

Reputation: 64004

One-line to import module using relative path

I have a module called in a code the following way:

import sys
sys.path.append("..")
import preprocessor

Is there a way to make into one line?

I tried this but failed:

from .. import preprocessor

I get this error

ValueError: Attempted relative import in non-package

Upvotes: 1

Views: 77

Answers (1)

Serjik
Serjik

Reputation: 10931

You should run it as package

This is my fie structure:

/test
   /path1
     __init__.py
     preprocessor.py
     /path2
         __init__.py
         prog1.py

preprocessor.py:

a = 12

prog1.py:

from ..preprocessor import a 
print a

command line:

 test$   python -m path1.path2.prog1

output:

 12

Upvotes: 2

Related Questions