473,750 Members | 2,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ImportError depending on the calling module

Hi,

Given the following directory structure:

---------
|-- Obj.py
|-- __init__.py
|-- foo
| |-- FooTest.py
| `-- __init__.py
`-- start1.py
---------

With the following content:

---------
$ cat Obj.py
class Obj(object):
pass

$ cat __init__.py
import Obj
__all__ = ['Obj']

$ cat foo/FooTest.py
from Obj import Obj

class FooTest(object) :
pass

$ cat foo/__init__.py
from FooTest import FooTest
__all__ = ['FooTest']

$ cat start1.py
from foo import FooTest
x = FooTest()
print "Works!"
sab@pcf2245:~/test$
---------

The test works:

---------
$ python start1.py
Works!
$
---------

However, if the module is imported elsewhere, e.g. in the following
directory structure:

---------
test
|-- Obj.py
|-- __init__.py
|-- foo
| |-- FooTest.py
| `-- __init__.py
`-- start1.py
test2
`-- start2.py
---------

Where start2.py has the following content:

---------
$ cd test2
$ cat start2.py
import sys, os.path
sys.path.insert (0,
os.path.abspath (os.path.join(o s.path.dirname( __file__), '..')))
import test
import test.foo
print 'Works!'
---------

It does not work:

---------
$ python start2.py
Traceback (most recent call last):
File "start2.py" , line 4, in <module>
import test.foo
File "/home/sab/test/foo/__init__.py", line 1, in <module>
from FooTest import FooTest
File "/home/sab/test/foo/FooTest.py", line 1, in <module>
from Obj import Obj
ImportError: No module named Obj
---------

How would you import the foo module correctly?

-Samuel

Sep 6 '07 #1
6 1788
On 6 sep, 08:47, Samuel <knipk...@gmail .comwrote:
Given the following directory structure:

---------
|-- Obj.py
|-- __init__.py
|-- foo
| |-- FooTest.py
| `-- __init__.py
`-- start1.py
---------
The container looks like a package (you didn't tell us the name).
Should be placed somewhere accessible thru sys.path
start1.py does NOT belong to the package, neither start2.py; place
them somewhere else. They should import the package as any other
client code, without doing any import tricks nor playing with
sys.path.

--
Gabriel Genellina

Sep 6 '07 #2
On Sep 6, 5:44 pm, Gabriel Genellina <gagsl-...@yahoo.com.a rwrote:
On 6 sep, 08:47, Samuel <knipk...@gmail .comwrote:
Given the following directory structure:
---------
|-- Obj.py
|-- __init__.py
|-- foo
| |-- FooTest.py
| `-- __init__.py
`-- start1.py
---------

The container looks like a package (you didn't tell us the name).
Should be placed somewhere accessible thru sys.path
But it is. I added the path to sys.path.
They should import the package as any other
client code, without doing any import tricks nor playing with
sys.path.
Why does it matter whether I install it in sys.path or whether
sys.path is modified? What's the difference?

What I am doing is I ship two modules in one tarball with my software.
The software is only unpacked and ran. It has to work without
installation.

-Samuel

Sep 6 '07 #3
On Sep 6, 6:13 pm, Samuel <knipk...@gmail .comwrote:
Why does it matter whether I install it in sys.path or whether
sys.path is modified? What's the difference?

What I am doing is I ship two modules in one tarball with my software.
The software is only unpacked and ran. It has to work without
installation.
I see now how this works; when a script is ran, only the top-level of
the current module is searched. Now that's just ugly, but well... at
least I now know how to work around this.

Thanks,
-Samuel

Sep 6 '07 #4
On 6 sep, 13:13, Samuel <knipk...@gmail .comwrote:
On Sep 6, 5:44 pm, Gabriel Genellina <gagsl-...@yahoo.com.a rwrote:
They should import the package as any other
client code, without doing any import tricks nor playing with
sys.path.

Why does it matter whether I install it in sys.path or whether
sys.path is modified? What's the difference?
Because it's more simple, less error prone, easier to test, more
efficient...
What I am doing is I ship two modules in one tarball with my software.
The software is only unpacked and ran. It has to work without
installation.
That's fine, and a good requirement. Place start2.py inside the
container directory, and your package beneath it. Then you don't have
to modify sys.path to find the package - "import packagename" just
works. And it still works if the user installs the package (into site-
packages, by example).

start1.py
start2.py
packagename/
|-- Obj.py
|-- __init__.py
|-- foo/
|-- FooTest.py
`-- __init__.py

Packages are... well, packages, boxes. Don't try to import a module
inside a package as it were a standalone module - relative imports
won't work then.
This was discussed some time ago <http://groups.google.com.ar/group/
comp.lang.pytho n/msg/b2d0c36e388e9ab 6>

--
Gabriel Genellina

Sep 6 '07 #5
On Sep 6, 6:44 pm, Gabriel Genellina <gagsl-...@yahoo.com.a rwrote:
On 6 sep, 13:13, Samuel <knipk...@gmail .comwrote:
On Sep 6, 5:44 pm, Gabriel Genellina <gagsl-...@yahoo.com.a rwrote:
They should import the package as any other
client code, without doing any import tricks nor playing with
sys.path.
Why does it matter whether I install it in sys.path or whether
sys.path is modified? What's the difference?

Because it's more simple, less error prone, easier to test, more
efficient...
What I am doing is I ship two modules in one tarball with my software.
The software is only unpacked and ran. It has to work without
installation.

That's fine, and a good requirement. Place start2.py inside the
container directory, and your package beneath it. Then you don't have
to modify sys.path to find the package - "import packagename" just
works. And it still works if the user installs the package (into site-
packages, by example).

start1.py
start2.py
packagename/
|-- Obj.py
|-- __init__.py
|-- foo/
|-- FooTest.py
`-- __init__.py

Packages are... well, packages, boxes. Don't try to import a module
inside a package as it were a standalone module
But the start2.py script *is* part of the library. It should be in the
package.
However, I just found that Python 2.5 introduces relative imports.
This sounds like an attempt to remedy the situation.

-Samuel

Sep 6 '07 #6
En Thu, 06 Sep 2007 14:41:42 -0300, Samuel <kn******@gmail .comescribi�:
On Sep 6, 6:44 pm, Gabriel Genellina <gagsl-...@yahoo.com.a rwrote:
>On 6 sep, 13:13, Samuel <knipk...@gmail .comwrote:

That's fine, and a good requirement. Place start2.py inside the
container directory, and your package beneath it. Then you don't have
to modify sys.path to find the package - "import packagename" just
works. And it still works if the user installs the package (into site-
packages, by example).

start1.py
start2.py
packagename/
|-- Obj.py
|-- __init__.py
|-- foo/
|-- FooTest.py
`-- __init__.py

Packages are... well, packages, boxes. Don't try to import a module
inside a package as it were a standalone module

But the start2.py script *is* part of the library. It should be in the
package.
If it imports the package, it cannot be part of the package itself.
However, I just found that Python 2.5 introduces relative imports.
This sounds like an attempt to remedy the situation.
For relative imports to work, the importing module must know that it is
part of a package. That is not the case if you run a script located inside
the package.

--
Gabriel Genellina

Sep 6 '07 #7

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

Similar topics

2
17714
by: x-herbert | last post by:
Hi, I have a small test to "compile" al litle script as a WMI-Tester. The script include a wmi-wrapper and "insert" the Win32-modeles. here the code: my "WMI-Tester.py" ----- import wmi
1
10736
by: Askari | last post by:
Hi, I have this error when I want snashot of my screen with : Traceback (most recent call last): File "C:\Documents and Settings\Assembleur_MAN\Bureau\CasseTete (PyGame).py", line 1121, in ? import ImageGrab File "C:\Python23\Lib\site-packages\PIL\ImageGrab.py", line 29, in ? grabber = Image.core.grabscreen File "C:\Python23\Lib\site-packages\PIL\Image.py", line 39, in
1
2066
by: flupke | last post by:
Hi, i am in search of a cddb module for Python on windows. I stumbled upon http://sourceforge.net/projects/cddb-py but couldn't use it since the mci.dll is compiled against Python2.0. So i'm trying to rebuild it using Dev-C++. I've succeeded in building a dll but when i try a test program it get this error: http://sourceforge.net/projects/cddb-py Traceback (most recent call last):
4
7561
by: Daniel | last post by:
Hello, I'm trying to build a very simple IPC system. What I have done is create Data Transfer Objects (DTO) for each item I'd like to send across the wire. I am serializing these using cPickle. I've also tried using pickle (instead of cPickle), but I get the same response. Below is the code. I'll put the rest of my comments after the code
0
9000
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9256
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6804
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4713
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
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 we have to send another system
2
2804
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.