1

suppose i have this directory structure

package /
         __init__.py
         cpackage.py

        subpackage1/
                    __init__.py
                    subpack1_call.py

                    /lib
                        __init__.py
                        sub_lib.py
        subpackage2/
                    __init__.py
                    subpack2_call.py

i want to import cpackage in subpackage1 and subpackage2 which i am unable to import i get valuename error and module not found errors

where as i can easily do this in subpackage1

from lib.sub_lib import hello_pr

hello_pr() 

here there is no error and hello_pr prints what i defined in sub_lib but i am unable to move up the directory, where as in above case i can easily move down the directory structure

what am i missing . i have looked into so many solutions in this site and pydoc, maybe i am missing something, cause nothing seemed to be working for

rgm
  • 1,241
  • 2
  • 16
  • 33

2 Answers2

1

After parsing and reparsing your question a few times, I've decided that what you're looking for is relative imports.

from ..cpackage import somename
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • this the error which i am seeing : File "subpackage1.py", line 1, in from ..cpackage import callsome_one ValueError: Attempted relative import in non-packag – rgm Jan 25 '13 at 07:59
  • You may have a look at http://stackoverflow.com/questions/5803781/py2exe-cannot-import-module-from-other-directory and http://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python and also http://www.py2exe.org/index.cgi/FAQ –  Jan 26 '13 at 20:55
1

If you can import lib.sub_lib, it means your PYTHONPATH points to subpackage1. It should point to the directory containing package, then you'll be able to import package.cpackage, package.subpackage1.lib.sub_lib, etc.

You can also point your PYTHONPATH to cpackage, then remove init.py in this directory as it's useless, and you can import cpackage, subpackage1.lib.sub_lib, etc.

The basic rule is: if PYTHONPATH=dir, then

dir\
  bob.py
  sub\
    __init__.py
    bib.py
    inner\
      __init__.py
      bub.py

import bob
import sub       (will import sub\__init__.py)
import sub.bib   (will import sub\__init__.py then bib.py)
import sub.inner (will import sub\__init__.py then sub\inner\__init__.py)
import sub.inner.bub (will import sub\__init__.py then sub\inner\__init__.py
                      and finally bub.py)
  • if set the pythonpath and then do py2exe of my project, will it cause any execution error in some other computer. ( forgive me my question is highly ridiculous) – rgm Jan 25 '13 at 08:24
  • When you build your application, py2exe finds all needed packages, even those which are accessed in PYTHONPATH, and they are packed in library.zip. However, the distributed exe file doesn't depend on PYTHONPATH (it won't look at it, to prevent problems if installed libraries conflict with your program). –  Jan 25 '13 at 08:53
  • so the packages will be in library.zip and distributed exe will look in library.zip. right? i dont have to change os.sys? – rgm Jan 25 '13 at 09:12
  • No, you don't have to change sys.path (at least for "simple" programs). –  Jan 25 '13 at 10:49
  • as you said when i added my dir to PYTHONPATH, everything worked properly. there was no problem until i created py2exe. my top layer imports in sublayer were not present, my project did not have all the imports properly casue when i ran the exe , i got importerror, but when i ran as python itself , all the imports worked properly , i even added in environment variable "path" in windows,still it did not work. – rgm Jan 26 '13 at 19:23
  • What do you exactly mean by "my top layer imports in sublayer were not present" ? –  Jan 26 '13 at 20:02
  • sorry for causing confusion. taking the above dir structure that you have given. "dir\folder1\file.py" this import is failing in "dir\sub\bib.py". Once again this happened only when i created py2exe and not when i ran with python scripts. – rgm Jan 26 '13 at 20:16
  • 1
    You mean you have PYTHONPATH=...\dir and you import sub.bib ? Well... I'm afraid I'm a bit confused too ;-) Anyway, I think it should work with py2exe, given it works with python. I must be missing something... –  Jan 26 '13 at 20:51
  • i found out what i was doing wrong ,i did not create "pythonpath" in windows sys env, i just added the dir structure to sys, now i created pythonpath env and in that i added the dir structure. and then i created my py2exe. worked perfectly. Thanks – rgm Jan 28 '13 at 10:54