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

asked Jan 25, 2013 at 7:09
0

2 Answers 2

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)
answered Jan 25, 2013 at 7:18
Sign up to request clarification or add additional context in comments.

9 Comments

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)
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).
so the packages will be in library.zip and distributed exe will look in library.zip. right? i dont have to change os.sys?
No, you don't have to change sys.path (at least for "simple" programs).
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...
|
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
answered Jan 25, 2013 at 7:17

2 Comments

this the error which i am seeing : File "subpackage1.py", line 1, in <module> from ..cpackage import callsome_one ValueError: Attempted relative import in non-packag

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.