473,394 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

"dynamical" importing

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
6 1370
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
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
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
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
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
Thanks a lot to all.

Oct 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Michael | last post by:
How can I determine a users PC Total system memory and memory available or free at the time of my program loading. Thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.