473,320 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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(os.path.dirname(__fil e__), '..')))
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 1766
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.arwrote:
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.arwrote:
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.python/msg/b2d0c36e388e9ab6>

--
Gabriel Genellina

Sep 6 '07 #5
On Sep 6, 6:44 pm, Gabriel Genellina <gagsl-...@yahoo.com.arwrote:
On 6 sep, 13:13, Samuel <knipk...@gmail.comwrote:
On Sep 6, 5:44 pm, Gabriel Genellina <gagsl-...@yahoo.com.arwrote:
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.arwrote:
>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
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
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 ?...
1
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...
4
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.