2

Relative import not working properly in python2.6.5 getting "ValueError: Attempted relative import in non-package".

I am having all those __init__.py in proper place.

nmichaels
  • 49,466
  • 12
  • 107
  • 135
user865438
  • 1,913
  • 2
  • 12
  • 8
  • 1
    (1) It's `__init__.py`, make sure you have that right. (2) Are you sure all of your `__init__` files are in the right place? Please post a list of your directory structure so we can help point out errors. – Patrick Perini Jul 27 '11 at 13:12
  • 2
    It's impossible to tell what the problem is unless you give us an overview of your directory structure and the offending code. – Rafe Kettler Jul 27 '11 at 13:13
  • You should probably show your import statements and your directory structure in order to receive help... – Constantinius Jul 27 '11 at 13:14
  • PythonEvent --main.py --__init__.py DBConnector -- __init__.py -- connector.py service -- __init__.py -- myservice.py Now in myservice.py i have a line like from ..DBConnector.connector import DBUpdate – user865438 Jul 27 '11 at 13:20

2 Answers2

2

I have seen that error before when running a script that is actually inside a package. To the interpreter, it appears as though the package is not a package.

Try taking the script into another directory, putting your package inside your pythonpath, and import absolutely. Then, relative imports inside your package will work. NOTE: you can STILL not relatively import inside the end script - the easiest thing to do in this case is to make a "wrapper" script, that simply calls some entry point in your package.

You can go even further here by using setuptools to create a setup.py for your package, to make it distributable. Then, as a part of that, entry points would allow you to autogenerate scripts that called your package's code.

EDIT:

From your comment, it appears as though I wasn't quite clear. I'm not 100% sure of your directory structure because your comment above wasn't formatted, but I took it to be like this:

PythonEvent/
    main.py
    __init__.py
    DBConnector/
        __init__.py
        connector.py
    service/
        __init__.py
        myservice.py

When in myservice.py you have the line from ..DBConnector.connector import DBUpdate, the interpreter tries to import it relatively, UNLESS you are running myservice.py directly. This is what it appears you are doing.

Try making another dummy script outside of PythonEvent/ that is simply as follows:

from PythonEvent.service import myservice

if __name__ == '__main__':
    myservice.main() # or whatever the entry point is called in myservice.

Then, set your PYTHONPATH environment variable to point to the parent directory of PythonEvent/ (or move PythonEvent/ to your site-packages).

Nate
  • 12,499
  • 5
  • 45
  • 60
0
main.py
setup.py
Main Package/ ->
    __init__.py
    subpackage_a/ ->
       __init__.py
       module_a.py
    subpackage_b/ ->
       __init__.py
       module_b.py

i)

    1.You run python main.py
    2.main.py does: import app.package_a.module_a
    3.module_a.py does import app.package_b.module_b

ii)

    Alternatively 2 or 3 could use: from app.package_a import module_a

That will work as long as you have app in your PYTHONPATH. main.py could be anywhere then.

So you write a setup.py to copy (install) the whole app package and subpackages to the target system's python folders, and main.py to target system's script folders.

Thanks to https://stackoverflow.com/a/1083169

Community
  • 1
  • 1
Cholavendhan
  • 337
  • 1
  • 5
  • 11