473,378 Members | 1,110 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,378 software developers and data experts.

Accessing global namespace

I'm trying to build a completely plug-in based system. One of my
problems is importing a package dynamically. I'm trying to emulate the
import command. The __import__() function or imp module doesn't help me
much because they only return the module. I want to register the module
with it's name in the current namespace. I can do it by:

globals()[module_name] = __import__(module_name)

But I don't think it's a good thing to access the global namespace
directly. I prefer using setattr() but I cannot access the current
namespace as an object I want something like

setattr(__main__,__import__(module_name))

Is that possible? I'm using Python 2.1 on Debian Woody

--
Love, Respect, Linux
################################################## ##########################
"Has anyone had problems with the computer accounts?"
"Yes, I don't have one."
"Okay, you can send mail to one of the tutors ..."
-- E. D'Azevedo, Computer Science 372
################################################## ##########################
Tonguç Yumruk

Jul 18 '05 #1
3 7617
Tonguç Yumruk wrote:
I'm trying to build a completely plug-in based system. One of my
problems is importing a package dynamically. I'm trying to emulate the
import command. The __import__() function or imp module doesn't help me
much because they only return the module. I want to register the module
with it's name in the current namespace. I can do it by:

globals()[module_name] = __import__(module_name)

But I don't think it's a good thing to access the global namespace
directly. I prefer using setattr() but I cannot access the current
namespace as an object I want something like

setattr(__main__,__import__(module_name))

Is that possible? I'm using Python 2.1 on Debian Woody


Try (tested with 2.2 and 2.3).

import sys

setattr(sys.modules[__name__], "myos", __import__("os"))

sys.modules is a dictionary of all imported modules. It even has a
"__main__" key.

Peter

Jul 18 '05 #2
Tonguç Yumruk wrote:

I'm trying to build a completely plug-in based system. One of my
problems is importing a package dynamically. I'm trying to emulate the
import command. The __import__() function or imp module doesn't help me
much because they only return the module. I want to register the module
with it's name in the current namespace. I can do it by:

globals()[module_name] = __import__(module_name)

But I don't think it's a good thing to access the global namespace
directly.


There's nothing wrong with "accessing the global namespace directly"
like that. In fact, that's pretty much the only way (though it's not
in itself sufficient) to emulate the "import" statement when you want
a dynamic import.

In general, you need to (a) import with __imoprt__, (b) insert name
into sys.modules dictionary, and (c) insert name into globals(). What
you are doing above is the proper way to do that, IMHO.

-Peter
Jul 18 '05 #3

"Tonguç Yumruk" <tr*****@ttnet.net.tr> wrote in message
news:ma**********************************@python.o rg...
I'm trying to build a completely plug-in based system. One of my
problems is importing a package dynamically. I'm trying to emulate the
import command. The __import__() function or imp module doesn't help me
much because they only return the module. I want to register the module
with it's name in the current namespace. I can do it by:

globals()[module_name] = __import__(module_name)

But I don't think it's a good thing to access the global namespace
directly. I prefer using setattr() but I cannot access the current
namespace as an object I want something like

setattr(__main__,__import__(module_name))

Is that possible? I'm using Python 2.1 on Debian Woody
The easiest way to do a dynamic import is to build an
import statement and feed it into the exec statement.

On the other hand, I'd question why you actually want
the name in the global namespace of your module. Might
putting it in some kind of data structure be better for your
program logic? I don't know enough about your application
to tell, though.

John Roth

--
Love, Respect, Linux
################################################## ########################## "Has anyone had problems with the computer accounts?"
"Yes, I don't have one."
"Okay, you can send mail to one of the tutors ..."
-- E. D'Azevedo, Computer Science 372
################################################## ########################## Tonguç Yumruk

Jul 18 '05 #4

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

Similar topics

88
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from...
12
by: Santiago de Compostela | last post by:
Hi The following program doesn't compile on MS VC++ or Bloodshed Dev-C++ #include <iostream> int strlen(const char *in) {
4
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
2
by: Tony Johansson | last post by:
Hello! I'm reading a book about C++ and there is something that I don't understand so I ask you. Below I have the text from the book and the code from the file where main is located and some...
12
by: Steve Blinkhorn | last post by:
Does anyone know of a way of accessing and modifying variables declared static within a function from outside that function? Please no homilies on why it's bad practice: the context is very...
3
by: Frederick Gotham | last post by:
Back in the day, if you wanted a function to be self-contained within a translation unit, you defined the function as "static". If there were an external linkage function by the same name...
3
by: mrstephengross | last post by:
Hi folks. I've got a weird situation--gcc doesn't like the folllowing code snippet, but I don't know if it's correct or not. Here's the situation: In the global namespace, I've got a operator<<...
12
by: reubendb | last post by:
Hello, I am new to Python. I have the following question / problem. I have a visualization software with command-line interface (CLI), which essentially is a Python (v. 2.5) interpreter with...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

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.