473,804 Members | 2,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

namespace problems

Hi all, I am trying to get the following to work, but cant seem to do
it the way i want to.

ok, so I come into python and do the following:
>>x = 1
then, i have a file called test.py in which i say:
print x

Now, after having defined x =1 in the python interpreter, i come in and
say:
import test
it has a problem, which i can understand, because it is in its own
namespace,
but even if i say:
from test import *
the interpreter still gives me the error:
NameError: name 'x' is not defined

the only way i can get it to work is if i say: execfile('./test.py')
how can i get this to work by importing and not execfile?

thanks for your help!

Aug 25 '06 #1
1 1272

"Kiran" <Ki*********@gm ail.comwrote in message
news:11******** *************@h 48g2000cwc.goog legroups.com...
Hi all, I am trying to get the following to work, but cant seem to do
it the way i want to.

ok, so I come into python and do the following:
>>>x = 1

then, i have a file called test.py in which i say:
print x

Now, after having defined x =1 in the python interpreter, i come in and
say:
import test
it has a problem, which i can understand, because it is in its own
namespace,
but even if i say:
from test import *
the interpreter still gives me the error:
NameError: name 'x' is not defined

the only way i can get it to work is if i say: execfile('./test.py')
how can i get this to work by importing and not execfile?
Dennis gave you one way: import __main__ into test.
Another way to come close is to put the print inside a function.

test.py
---------
def p(): print x

Then your script could say

import test
test.x = 1
test.p()

If you don't already, you need to know that import only executes the code
for a module the *first* time you import it. So normally, top level code
in modules consists of function and class definitions and perhaps the
initialization of a few module-level constants or variables.

Terry Jan Reedy


Aug 25 '06 #2

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:
3
7657
by: Tonguç Yumruk | last post by:
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() = __import__(module_name) But I don't think it's a good thing to access the global namespace...
20
3874
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in a named namespace namespace foo { class Foo
5
7393
by: Alexander Gnauck | last post by:
Hello, i have problems with the Namespaces and the .Net XML Parser My XML looks like this: <query xmlns="jabber:iq:roster"> <item jid="srlee@localhost" name="srlee" subscription="both"><group>contacts</group></item> <item jid="tlee@localhost" name="Troy" subscription="both"><group>contacts</group></item>
1
2061
by: Phill. W | last post by:
Having changed a VB.Net(2003) DLL project, I'm getting Type Loading problems in other code that uses my DLL. In my source code, I have a Namespace Statement something like this: Namespace XYZ.AProject but when I build the DLL project and open the resulting DLL up in ILDASM, the Namespace has been *translated* to "XYZ.Aproject".
32
30511
by: toolmaster | last post by:
Since many of the modern computer languages have built-in namespace features, I can't understand why not add this feature into standard C. I've heard many people complain of the lacking of namespace in C. Of course, namespace doesn't eliminate name polution, but it helps more than just to put some prefix before a function name. Is it because adding namespace will make C more complex or some other reasons?
7
4582
by: zahy[dot]bnaya[At]gmail[dot]com | last post by:
Hi all, Since I am always confusing this, I want to know once and for all what is the right way of doing this. I've noticed that some programs use: std::cout<< "yadayada"<<endl;
30
4126
by: Pep | last post by:
Is it best to include the code "using namespace std;" in the source or should each keyword in the std namespace be qualified by the namespace tag, such as std::cout << "using std namespace" << std::endl; Myself I am not sure which I prefer, it is certainly easier to specify that the std namespace is being used instead of tagging each member of the namespace?
3
2869
by: sjt003 | last post by:
I have been developing web apps in Visual Studio 2003, but since the other developers in my office don't use Visual Studio, I may have to stop too unless there is an efficient way for them to compile the code outside of Visual Studio if they need to adjust something I created. I have been trying to use vbc.exe, but I'm having problems with references to the DataSet I have created. Multiple pages reference my dataset - "DataSet1", ......
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...
0
9595
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
10604
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
10354
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
9177
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
7643
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
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.