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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
setuptoolsto create asetup.pyfor 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:
When in
myservice.pyyou have the linefrom ..DBConnector.connector import DBUpdate, the interpreter tries to import it relatively, UNLESS you are runningmyservice.pydirectly. This is what it appears you are doing.Try making another dummy script outside of
PythonEvent/that is simply as follows:Then, set your
PYTHONPATHenvironment variable to point to the parent directory ofPythonEvent/(or movePythonEvent/to your site-packages).