Problem with Packages

S

Srikanth

Hi,

I am learning about Python packages and I am getting an ImportError
but I can't figure out the reason why.

I have three modules and they are stored in a directory hierarchy as:

my_apps
|
|--> mod3.py
|--> dir1/dir1_1/mod1.py
|--> dir2/dir2_2/mod2.py

mod1.py defines a function called add(num1, num2) and mod2.py defines
a function called multiply(num1, num2) and mod3.py makes use of the
two functions defined in mod1.py and mod2.py and mod3.py's contents is
as follows:

-----------------------------------------------
from dir1.dir1_1 import mod1
from dir2.dir2_2 import mod2

def main():
print mod1.add(10, 34)
print mod2.multiply(3, 6)

if __name__ == "__main__":
main()
----------------------------------------------

And the directory my_apps is included in PYTHONPATH environment
variable, what am I doing wrong? I executed this code under PyDev and
it's working perfectly but when I run it outside of PyDev, it fails.

This is command I used to execute mod3.py:

C:\> python C:\my_apps\mod3.py

But I get the following error:
Traceback (most recent call last):
File "c:\my_apps\mod3.py", line 1, in ?
from dir1.dir1_1 import mod1
ImportError: No module named dir1.dir1_1

Thanks,
Srikanth
 
G

Gabriel Genellina

I am learning about Python packages and I am getting an ImportError
but I can't figure out the reason why.

It appears that you forgot the basic rule: a package is a directory with
an __init__.py file (even if empty).
I have three modules and they are stored in a directory hierarchy as:

my_apps
|
|--> mod3.py
|--> dir1/dir1_1/mod1.py
|--> dir2/dir2_2/mod2.py

You need 4 of such files here.
And the directory my_apps is included in PYTHONPATH environment
variable, what am I doing wrong? I executed this code under PyDev and
it's working perfectly but when I run it outside of PyDev, it fails.

Maybe PyDev plays some tricks with PYTHONPATH or something...
 
S

Srikanth

It appears that you forgot the basic rule: a package is a directory with
an __init__.py file (even if empty).

Exactly right. I didn't know that __init__.py is a mandatory one.
Thanks for pointing out.
You need 4 of such files here.

Thanks again for saying 4, cause I would have definetly put only 2
(inside dir1_1 and dir2_2)
Maybe PyDev plays some tricks with PYTHONPATH or something...

I got the same doubt and I checked PyDev before posting, the
PYTHONPATH is exactly the same but I didn't notice or rather
overlooked __init__.py file it created automatically when I created
packages using the dialogs.
Gabriel Genellina

Thanks you Gabriel!
-- Srikanth
 
G

Gabriel Genellina

Exactly right. I didn't know that __init__.py is a mandatory one.
Thanks for pointing out.

You may want to review this section on the Python Tutorial, covering
packages:
http://docs.python.org/tut/node8.html#SECTION008400000000000000000
Thanks again for saying 4, cause I would have definetly put only 2
(inside dir1_1 and dir2_2)

That was absolutely intentional :) so you had to figure out *where* to put
the __init__.py files...
I got the same doubt and I checked PyDev before posting, the
PYTHONPATH is exactly the same but I didn't notice or rather
overlooked __init__.py file it created automatically when I created
packages using the dialogs.

Ah, those automagical tools...
Thanks you Gabriel!

I'm glad you found it helpful!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top