472,958 Members | 2,260 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Why can't I "from module import *" except at module level?

I have a directory structure that contains different modules that run
depending on what the user selects. They are identical in name and
structure, but what varies is the content of the functions. They will
only need to be run once per execution.

Example (directory level):
Sys1:
A
B
C
Sys2:
A
B
C
Sys3:
A
B
C
Sys4:
A
B
C

So if the user selects Sys1 during execution, I want to import the
modules using "from *" and the run the content of those files.

Now the way my program is set up, it is very important that I be able
to "from Sys1 import *" and not "import Sys1". The names of the
functions inside are passed in from somewhere else and the program
requires those function names to be globally scoped.

So I have a function that looks like this:

def importModules( type ):
cwd = os.getcwd()
path = cwd + "\\" + type
sys.path.append(path)

from security import *

Obviously this is not working and I get a syntax error at runtime. So
without this functionality, how do I target modules to import in other
directories after program execution has begun?

Thanks

Jan 13 '06 #1
3 5305
Anyone?

Is there any way to do this or am must I load all modules by function
name only if it's after initialization?

Jan 13 '06 #2
Mudcat wrote:
Is there any way to do this or am must I load all modules by function
name only if it's after initialization?


Not sure. globals().update(mod.__dict__) might do the trick. Or just design a
better system and be done with it.
--
Giovanni Bajo
Jan 14 '06 #3
On Fri, 13 Jan 2006 10:17:32 -0800, Mudcat wrote:
I have a directory structure that contains different modules that run
depending on what the user selects. They are identical in name and
structure, but what varies is the content of the functions. They will
only need to be run once per execution. .... So if the user selects Sys1 during execution, I want to import the
modules using "from *" and the run the content of those files.

Now the way my program is set up, it is very important that I be able
to "from Sys1 import *" and not "import Sys1".
Then change the way your program is set up :-)

Seriously, "from module import *" is generally a bad idea. Not always, but
generally, and in this specific instance I'm not sure that your use case
is one of those exceptions. However, be that as it may be, on to solving
your problem:

When you "import foo" or "from module import foo", the name foo is bound
in the local scope, not global. In other words, the name foo will then
only exist inside the function. So this does not work:
def local_import(): .... import string
.... local_import()
string.whitespace Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'string' is not defined
However this does work as you would expect:
def func_import(): .... global math
.... import math
.... func_import()
math.sin <built-in function sin>
See http://docs.python.org/ref/import.html for more detail on what happens
when you import. Keep in mind that it states that "from module import *"
in function blocks is explicitly stated to be an error, and in Python 2.3
a warning is raised:

def wild_import():

.... from math import *
....
<stdin>:1: SyntaxWarning: import * only allowed at module level

The names of the
functions inside are passed in from somewhere else and the program
requires those function names to be globally scoped.

So I have a function that looks like this:

def importModules( type ):
cwd = os.getcwd()
path = cwd + "\\" + type
sys.path.append(path)

from security import *

Obviously this is not working and I get a syntax error at runtime.
Should security be defined somewhere?
So
without this functionality, how do I target modules to import in other
directories after program execution has begun?


I'd consider putting your import logic into a module of its own, at
the module-level and not inside a function. Then just call "from importer
import *" in the top level of your code and I think that should meet your
needs.

--
Steven.

Jan 14 '06 #4

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

Similar topics

12
by: Georg Brandl | last post by:
Hello, in follow-up to the recent "dictionary accumulator" thread, I wrote a little module with several subclassed dicts. Comments (e.g. makes it sense to use super), corrections, etc.? Is...
3
by: gerald.maher | last post by:
Hi, I am trying to excuate the follwong code: I get the following error: Here is my Lib path:
5
by: Bob | last post by:
Are they different names for the same concept ?
3
by: Chris | last post by:
Hi, 1) In file test.aspx, i put: <%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %> <%@ import namespace="System.Data"%> <%@ import...
5
by: mark_galeck_spam_magnet | last post by:
Hi, why does complain name 'compileFile' not defined. But works. Why? (I did read the tutorial, it seems to say "import module" should work. Thank you, Mark
1
by: lemke_juergen | last post by:
Hi everyone, I define some vars and functions in a "support" module which gets called from my main app module. Using Python 2.5. I import all symbols in the support module at the top of the...
0
by: Shehryar | last post by:
AOA, I urgently want to know the major difference between "Module" and a "Class Module". Plz, reply me as early as possible. Thanx!
2
by: Gabriele *darkbard* Farina | last post by:
Hi, I'm using Python 2.5 to develop a simple MVC framework based on mod_python. To load my controllers, I create new modules using the "new" module like this: # .... my_module =...
1
by: Malcolm Greene | last post by:
Is there any consensus on what "from __future__ import" options developers should be using in their Python 2.5.2 applications? Is there a consolidated list of "from __future__ import" options to...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.