By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,374 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

"dynamical" importing

P: n/a
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

Oct 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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


Oct 19 '05 #2

P: n/a
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>

Oct 19 '05 #3

P: n/a
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


Oct 19 '05 #4

P: n/a
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>

Oct 19 '05 #5

P: n/a
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
Oct 19 '05 #6

P: n/a
Thanks a lot to all.

Oct 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.