Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > "dynamical" importing

Reply
Thread Tools

"dynamical" importing

 
 
Joerg Schuster
Guest
Posts: n/a
 
      10-19-2005
Hello,

I need to import modules from user defined paths. I.e. I want to do
something
like:

module_dir = sys.argv[1]

my_path = os.path.join(module_dir, 'bin', 'my_module')

from my_path import my_object

Obviously, it doesn't work this way. How would it work?

Jörg Schuster

 
Reply With Quote
 
 
 
 
Laurent Rahuel
Guest
Posts: n/a
 
      10-19-2005
Hi,

I guess you need to look at __import__

Regards,

Laurent.

Joerg Schuster wrote:

> Hello,
>
> I need to import modules from user defined paths. I.e. I want to do
> something
> like:
>
> module_dir = sys.argv[1]
>
> my_path = os.path.join(module_dir, 'bin', 'my_module')
>
> from my_path import my_object
>
> Obviously, it doesn't work this way. How would it work?
>
> Jörg Schuster


 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      10-19-2005
Joerg Schuster wrote:

> I need to import modules from user defined paths. I.e. I want to do
> something like:
>
> module_dir = sys.argv[1]
>
> my_path = os.path.join(module_dir, 'bin', 'my_module')
>
> from my_path import my_object
>
> Obviously, it doesn't work this way. How would it work?


some alternatives:

- if you want the modules to remain imported:

try:
sys.path.insert(0, os.path.join(module_dir, "bin"))
module = __import__("my_module")
finally:
del sys.path[0]
object = module.my_object

- if you're only interested in the object:

namespace = {}
execfile(os.path.join(module_dir, "bin", "my_module" + ".py"), namespace)
object = namespace["my_object"]

</F>



 
Reply With Quote
 
John Abel
Guest
Posts: n/a
 
      10-19-2005
Try:

userModule = _importModule( pathToModule )

def _importModule( moduleName ):
modName = __import__ ( moduleName )
modComponents = moduleName.split( '.' )
for indivComp in modComponents[ 1: ]:
modName = getattr( modName, indivComp )

return modName

HTH,

J

Joerg Schuster wrote:

>Hello,
>
>I need to import modules from user defined paths. I.e. I want to do
>something
>like:
>
>module_dir = sys.argv[1]
>
>my_path = os.path.join(module_dir, 'bin', 'my_module')
>
>from my_path import my_object
>
>Obviously, it doesn't work this way. How would it work?
>
>Jörg Schuster
>
>
>


 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      10-19-2005
John Abel wrote:

> def _importModule( moduleName ):
> modName = __import__ ( moduleName )
> modComponents = moduleName.split( '.' )
> for indivComp in modComponents[ 1: ]:
> modName = getattr( modName, indivComp )
>
> return modName


__import__ takes a module name, not an arbitrary file name.

</F>



 
Reply With Quote
 
John Abel
Guest
Posts: n/a
 
      10-19-2005
Fredrik Lundh wrote:

>John Abel wrote:
>
>
>
>>def _importModule( moduleName ):
>> modName = __import__ ( moduleName )
>> modComponents = moduleName.split( '.' )
>> for indivComp in modComponents[ 1: ]:
>> modName = getattr( modName, indivComp )
>>
>> return modName
>>
>>

>
>__import__ takes a module name, not an arbitrary file name.
>
></F>
>
>
>
>
>

Didn't mean to imply that it did. By pathToModule, I meant spam.ham as
in
http://localhost/documentation/Pytho...ncs.html#l2h-6
( hopefully, that should explain things to the OP ).

J
 
Reply With Quote
 
Joerg Schuster
Guest
Posts: n/a
 
      10-19-2005
Thanks a lot to all.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to keep a module with the same name as a module it is importing from importing itself? plb Python 2 02-08-2005 03:14 PM
Importing Outlook Mail folders and Contacts? William W. Plummer Firefox 1 07-02-2004 04:25 PM
ThunderBird- importing email from Pegasus _ Firefox 1 06-22-2004 04:33 PM
Importing mail messages from a directory of Mozilla 1.4 to 1.5 mrzac Firefox 2 12-23-2003 05:45 PM
Still need help importing address book Axl Firefox 2 12-16-2003 09:45 AM



Advertisments