473,473 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Re: imported module no longer available

Jeff Dyke wrote:
>actually no, the only things in that fucntion were.
print globals().keys() - i see it here
print mymodulename - it fails here.

the `import mymodulename` statement is at the very top of the file.

plus the processing that was attempted after.
so how did that processing use the "mymodulename" name?
>in fact in the calling
method i was able to execute print mymodulename and it printed the
expected python output.
the calling method has nothing to do with what's considered to be a
local variable in the method being called, so that only means that the
name is indeed available in the global scope.
>So i went back to check that the name 'mymodulename' was not getting
overwritten by something else and the error went away. I've been
working on something else entirely for the past few hours and have
changed none of the code...and now it works. which is even more
troublesome then the error itself.
more likely, it indicates that you removed the line that caused Python
to treat that name as a local variable.
>Follow on question. If this name, mymodulename, was imported in some
other module.fucntion local to a function like
def anotherfunc():
import mymodulename

would that remove it from the globals() and save it to a locals() ? I
would assume the answer to be no.
even after reading the page I pointed you to?

import binds a name, so an import statement inside a function will cause
Python to treat that name as a local variable (unless you add a global
declaration to that function).

maybe a few examples will make this clearer; the following snippets are
complete programs:

snippet 1:

import module # adds module to the global namespace

def func():
module.func() # uses module from the global namespace

func() # no error here

snippet 2:

def func():
import module # adds module to the *local* namespace
module.func()

func() # no error here
module.func() # doesn't work; no module in global namespace

snippet 3:

def func():
global module # marks module as a global name
import module # adds module to the *global* namespace
module.func()

func() # no error here
module.func() # no error here; global module set by function

snippet 4:

import module # adds module to global namespace

def func():
import module # adds module to local namespace too
print module # prints local variable
module = None # sets local variable to None

func() # no error here
module.func() # no error here either; uses global namespace

snippet 5:

import module

def func():
print module # fails with an UnboundLocalError.
# lots of lines
import module # adds to local namespace; marks name as local
# some more code

func() # will fail at print statement

my guess is that the last snippet corresponds to your case.

</F>

Jul 21 '08 #1
0 2110

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

Similar topics

5
by: James Tauber | last post by:
Had a question from a colleague that I embarrassingly couldn't answer. He has a script, foo.py with a global. He wants to import bar.py and needs that global available in bar.py The following...
3
by: Jason | last post by:
Hi, I've been having trouble understanding the difference between global namespace within and between modules. For example, I can program a subthread to see a global name (a threaded event to...
4
by: Martin M. | last post by:
Hi, I have the following question: How can an imported module see/find the path to itself? Background: From my main script I import a module which needs a file (AppleScript) located in the...
10
by: Michael Abbott | last post by:
It seems to be an invariant of Python (insofar as Python has invariants) that a module is executed at most once in a Python session. I have a rather bizzare example that breaks this invariant: can...
1
by: Jeff Dyke | last post by:
I've come across an error that i'm not yet able to create a test case for but wanted to get see if someone could shed light on this. I have imported a module at the top of my file with import...
0
by: Jeff Dyke | last post by:
my apologies, to Fredrick, my response when solely to him. reply below, hopefully keeping thread intact. On Mon, Jul 21, 2008 at 12:28 PM, Jeff Dyke <jeff.dyke@gmail.comwrote:
3
by: Mohamed Yousef | last post by:
Hello , The problem I'm asking about is how can imported modules be aware of other imported modules so they don't have to re-import them (avoiding importing problems and Consicing code and...
0
by: Mohamed Yousef | last post by:
Hello , The problem I'm asking about is how can imported modules be aware of other imported modules so they don't have to re-import them (avoiding importing problems and Consicing code and...
0
by: Gabriel Genellina | last post by:
En Sun, 24 Aug 2008 07:34:41 -0300, Mohamed Yousef <harrrrpo@gmail.comescribió: Yes. That way, when you see a name "foo" used in a module, you can look at the imports to see where it comes from....
0
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,...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
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...

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.