473,809 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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__(modu le_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__(mo dule_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 7657
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__(modu le_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__(mo dule_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.mod ules[__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__(modu le_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******** *************** ***********@pyt hon.org...
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__(modu le_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__(mo dule_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
5158
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 the days before PHP got object-oriented features. For instance we currently have:
12
2003
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
7157
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
7517
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 namespace definition with class definitions. The book says "C++ has a global anonymous namespace that is similar to Java's global anonymous package. All declarations not explicitly placed in named
12
11725
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 particular and involves automatically generated code. I know several other ways of attacking my problem, but this would be the cleanest if it could be made to work. A little more context. I use C as the output of a code generating system which...
3
7848
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 residing in a different translation unit, then the current translation unit was simply oblivious to it and had no way of accessing it. Any time the function name was mentioned in the current translation unit, it referred to the "static" one which...
3
2625
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<< declared that will send a vector<Tto a std::ostream. In the "outer" namespace, I've got a operator<< declared that will send a Thing<Tto a std::ostream. In the "outer" namespace, I've got a function "foo" that tries to send a vector<Tto a...
12
1823
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 functions added to the global namespace. I would like to keep my own functions in a separate module and then import that module to the main script (that will be executed using the CLI interpreter). The problem is, I cannot access the functions in the...
1
29393
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 called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10643
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10121
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.