__import__ function broken in 2.6

P

Paul

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 <[email protected]>
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
 
P

Paul

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

Dave Angel

Paul said:
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 <[email protected]>
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
"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
 
C

Carl Banks

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
 
F

Frank Millman

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
 

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

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top