Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

__import__ function broken in 2.6

14 views
Skip to first unread message

Paul

unread,
Apr 25, 2009, 5:37:12 PM4/25/09
to
Hi

It seems in 2.6 you are no longer able to use the __import__ function
with different paths. Here is our code:

sys.path = g.origSysPath[:] # copy, not reference
sys.path.insert(0, modulePath)

sys.modules = g.origSysModules.copy()
if sys.modules.get(moduleName):
del sys.modules[moduleName]

# look for modules in subdirectories
moduleName = "module_"+moduleName+"/"+moduleName

module = __import__(moduleName)

Unfortunately, this no longer works in 2.6. Does anyone have any idea
on how to make it work with file paths?

After quite a lot of searching, I was actually able to find a patch
that someone made to fix this. Here is the code:

sfepy.googlecode.com/issues/attachment?
aid=-8587746048072650671&name=import.patch

---------------------------------------------------------------------------

commit 2e92019ef957b547856e81a144db6845bf95d881
Author: Robert Cimrman <cimr...@ntc.zcu.cz>
Date: Thu Mar 5 09:59:59 2009 +0100

fixed load_classes() for Python 2.6

- __import__() function does not work when passing a file path as
name

diff --git a/sfepy/terms/__init__.py b/sfepy/terms/__init__.py
index 3fab007..34a31a6 100644
--- a/sfepy/terms/__init__.py
+++ b/sfepy/terms/__init__.py
@@ -10,9 +10,9 @@ def load_classes( filenames, is_class ):
table = {}
for filename in filenames:
name = os.path.splitext( filename )[0]
-# print filename, name
- mod = __import__( name )
-# print mod
+ parts = name.split( os.path.sep )
+ mod, name = '.'.join( parts ), parts[-1:]
+ mod = __import__( mod, globals(), locals(), name )
for key, var in mod.__dict__.iteritems():
if is_class( key ):
table[var.name] = var

---------------------------------------------------------------------------

However, after a lot of messing around with the new __import__( mod,
globals(), locals(), name ) function, I am still unable to make it
work. Perhaps I am just not able to understand exactly what is going
on here.

Can anyone offer some assistance?

Thank you,
Paul

Paul

unread,
Apr 25, 2009, 7:38:56 PM4/25/09
to
We found a quick workaround to make import work with paths. We just
append the folder we want to the system's path:

sys.path.append( moduleFolder )

If anyone has a better solution, that would be great. Until then, this
is ugly, but it works.

Dave Angel

unread,
Apr 25, 2009, 7:52:14 PM4/25/09
to pythonlist
"No longer works" is pretty vague. If you get an error, how about
showing us just what it is. The following line builds a name with a
slash in it. What's the point? If the module is in a subdirectory, add
that subdirectory to modulePath.

moduleName = "module_"+moduleName+"/"+moduleName

DaveA

Carl Banks

unread,
Apr 25, 2009, 9:22:26 PM4/25/09
to
On Apr 25, 2:37 pm, Paul <Paul22...@gmail.com> wrote:
> It seems in 2.6 you are no longer able to use the __import__ function
> with different paths.

That functionality was deliberately removed, since it was never
intended to be present in the first place, and it only "worked" before
by accident. To get that behavior:

1. Insert the directory where it's located into sys.path. (And be
wary of module name conflicts.)

2. If it's a small config-type file, then execfile() would

As for why it was removed, it's because importing is designed to work
on package names, not filenames. (Given that Python's importing
framework is so complicated, though, one wonders whethter sticking to
a design ideal is worth it.)


Carl Banks

Frank Millman

unread,
Apr 26, 2009, 5:31:00 AM4/26/09
to
On Apr 25, 11:37 pm, Paul <Paul22...@gmail.com> wrote:
> Hi
>
> It seems in 2.6 you are no longer able to use the __import__ function
> with different paths.

I did not study your code in detail, so I may have missed something,
but can't you use the 'imp' module for this? That still works in 2.6,
and seems to be present in 3.1 as well.

Frank Millman

0 new messages