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

how can I import a module without using pythonpath?

P: n/a
Phd
Hi,

I'm using python 2.2, I want to import a module by referring to its
relative location. The reason for this is that there is another module
with the same name that's already in pythonpath( not my decision, but I
got to work around it, bummer). So is there any easy way to do it?

something like
import module "from this location"
Thanks!
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Phd wrote:
Hi,

I'm using python 2.2, I want to import a module by referring to its
relative location. The reason for this is that there is another module
with the same name that's already in pythonpath( not my decision, but I
got to work around it, bummer). So is there any easy way to do it?


import sys, os
sys.path.insert(0,os.path.abspath("relative path"))
import module
sys.path.remove(os.path.abspath("relative path"))
Jul 18 '05 #2

P: n/a
Phd
Nice, thanks so much!

Doug Holton wrote:
Phd wrote:
Hi,

I'm using python 2.2, I want to import a module by referring to its
relative location. The reason for this is that there is another module
with the same name that's already in pythonpath( not my decision, but
I got to work around it, bummer). So is there any easy way to do it?

import sys, os
sys.path.insert(0,os.path.abspath("relative path"))
import module
sys.path.remove(os.path.abspath("relative path"))

Jul 18 '05 #3

P: n/a
Doug Holton wrote:
I'm using python 2.2, I want to import a module by referring to its relative location. The reason
for this is that there is another module with the same name that's already in pythonpath( not my
decision, but I got to work around it, bummer). So is there any easy way to do it?


import sys, os
sys.path.insert(0,os.path.abspath("relative path"))
import module
sys.path.remove(os.path.abspath("relative path"))


a try-finally clause is a pretty good idea, in this case.

you might also wish to store the calculated path, in case the module
changes the current directory. or store the original sys.path, in case
the module messes with the path but shouldn't. (on the other hand, if
the module is nice and well behaved, you can replace the last line with
del sys.path[0]...)

</F>

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.