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

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 2105

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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
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,...
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...
0
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
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...

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.