473,498 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with Packages

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

Mar 8 '07 #1
3 1045
En Thu, 08 Mar 2007 02:37:51 -0300, Srikanth <sr*****@gmail.comescribió:
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...

--
Gabriel Genellina

Mar 8 '07 #2
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.
my_apps
|
|--mod3.py
|--dir1/dir1_1/mod1.py
|--dir2/dir2_2/mod2.py

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)
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...
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

Mar 8 '07 #3
En Thu, 08 Mar 2007 05:26:45 -0300, Srikanth <sr*****@gmail.comescribió:
>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 may want to review this section on the Python Tutorial, covering
packages:
http://docs.python.org/tut/node8.htm...00000000000000
>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)
That was absolutely intentional :) so you had to figure out *where* to put
the __init__.py files...
>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.
Ah, those automagical tools...
Thanks you Gabriel!
I'm glad you found it helpful!

--
Gabriel Genellina

Mar 8 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2967
by: RJS | last post by:
Hi all, I can't get a py2exe compiled app to run with numarray (numarray-0.5.win32- py2.2). Also wxPythonWIN32-2.3.3.1-Py22 and ActivePython-2.2.1-222. In the sample below, commenting out...
0
1742
by: Jose Vicente Nunez Z | last post by:
Greetings, I wrote a couple of custom dummy extensions in Python (one a pure Python and the other a C) and i managed to compile and install them without a problem: $ make python2 setup.py...
3
2258
by: ketulp_baroda | last post by:
Hi I am having a problem in appending to sys.path I am doing it this way: >>> sys.path >>> sys.path.append(r'D:\Python23\Lib\site-packages\code') >>> sys.path But when I close the shell...
2
9196
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
0
2531
by: Rafal Zawadzki | last post by:
Hi. I tried earlier to write python zsi mail list, but nobody answered. I am using ZSI 1.7/2.0rc1 with TTPro Soap SDK. The wsdl file can be found here: http://demo.seapine.com/ttsoapcgi.wsdl ...
1
1951
by: MakaMaka | last post by:
Hi All, I'm having a problem with Stani's Python Editor (SPE) that I'm hoping somebody has run into before. If I create a simple hello world program: print "hello" and try and run it in...
0
2032
by: Manuzhai | last post by:
Hello there, I have this weird problem with a mod_python application. Recently I installed ElementTree and cElementTree through ez_setup.py, even though they were already installed normally...
1
1542
by: aznach | last post by:
Hello! I have a shared hosting account at GrokThis.net and have a problem with the module order of the Python search path. I'd like to use django's svn trunk instead of the...
7
12669
by: siggi | last post by:
Hi all, when I do >>>sys.path in IDLE (winXP), i get a horrendously long list of paths, paths I may have used during a lot of trials and errors. How can I clean up sys.path? I mean, trim it of...
2
3240
by: jiang.haiyun | last post by:
Hi, I am having some serious problems with PyQT4, when i run pyqt script, I always get 'Segmentation fault'. the script is simple: ====================== %less qttest.py from PyQt4 import...
0
7121
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6993
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7197
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6881
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4899
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.