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

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 1261

"Kiran" <Ki*********@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.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
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...
3
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...
20
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...
5
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"...
1
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...
32
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...
7
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
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" <<...
3
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...
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<<...
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
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?
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
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...

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.