473,394 Members | 1,746 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.

Using the imp module

I understand that you can use the imp module to programmatically mimic
the "import xyzzy" statement.

But is there any way to programmatically mimic the "from xyzzy import
*" statment?

--
Claus Tondering

Oct 10 '06 #1
5 1428
Claus Tondering wrote:
>I understand that you can use the imp module to programmatically mimic
the "import xyzzy" statement.
"imp" sounds like overkill for that purpose; the usual way is do do that is to
explicitly call __import__:

xyzzy = __import__("xyzzy")
But is there any way to programmatically mimic the "from xyzzy import
*" statment?
you can use getattr() to pick out the objects your need. or you could do
something like:

globals().update(vars(__import__("xyzzy")))

</F>

Oct 10 '06 #2
Claus Tondering wrote:
I understand that you can use the imp module to programmatically mimic
the "import xyzzy" statement.

But is there any way to programmatically mimic the "from xyzzy import
*" statment?
See the docs for __import__().

I think imp is pretty much dead nowadays.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Oct 10 '06 #3
Fredrik Lundh wrote:
globals().update(vars(__import__("xyzzy")))
Thank you, I can use that in some cases. However, I need to import a
file that is not in the load path. The imp module allows me to specify
a path name; but as far as I know, the __import__ function does not
allow me to do that.

--
Claus Tondering

Oct 10 '06 #4
I wrote:
Fredrik Lundh wrote:
globals().update(vars(__import__("xyzzy")))

Thank you, I can use that in some cases. However, I need to import a
file that is not in the load path. The imp module allows me to specify
a path name; but as far as I know, the __import__ function does not
allow me to do that.
Oops, I forgot to add that I realize that the update(vars(...)) feature
works well with the imp module too.

--
Claus Tondering

Oct 10 '06 #5
Claus Tondering wrote:
Thank you, I can use that in some cases. However, I need to import a
file that is not in the load path.
that's pretty straightforward:

path = list(sys.path)
sys.path.insert(0, "directory")
try:
module = __import__("module")
finally:
sys.path[:] = path # restore

or, often as useful,

namespace = {}
execfile("directory/module.py", namespace)

the latter executes the external code everything you run it (unlike import,
which uses the sys.modules cache), and puts everything defined by the
external script into the given dictionary.

to import all that into your own module, you can do:

globals().update(namespace)

for extra bonus, you may want to check the __all__ attribute, and if that's
not present, filter out anything that starts with an underscore:

all_names = namespace.get("__all__")
if all_names is None:
all_names = (key for key in namespace where key[0] != "_")
my_namespace = globals()
for name in all_names:
my_namespace[name] = namespace[name]

</F>

Oct 10 '06 #6

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

Similar topics

1
by: David | last post by:
I have this error message poping up when I try to import a module I made in C using the Python/C API. Everything compiles like a charm. Gives me this error message : Traceback (most recent...
1
by: orit | last post by:
I have the following xml file: <?xml version="1.0" encoding="utf-8" ?> <course id="2555" title="Developing Microsoft .NET Applications for Windows (Visual C# .NET)" length="5 days"...
1
by: Venky | last post by:
I'm compiling a C program that is using Interbase 6.0 APIS. Getting the following errors at the time of linking. Linking test.exe: Linker Warning: No module definition file specified: using...
13
by: Bijoy Naick | last post by:
My project contains multiple aspx pages. Many of these pages have code-behind that use several helper functions. Instead of copying each helper function into each aspx page, I am thinking of...
2
by: Martin v. Löwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version:...
5
by: pyapplico | last post by:
Is there any possible way that I can place a .py file on the internet, and use that source code in an .py file on my computer?
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
18
by: Angus | last post by:
Hello We have a lot of C++ code. And we need to now create a library which can be used from C and C++. Given that we have a lot of C++ code using classes how can we 'hide' the fact that it is...
3
by: Tony Johansson | last post by:
Hello! You can set target Module for AttributeUsage. I just wonder what does it mean with module ? //Tony
11
by: minishilpi | last post by:
I have a question - I have this code below to send an email and I have referenced the Microsoft CDO Library 2000. It doesn't throw any exception in the console window. It goes throughout the code...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.