[Tutor] How to use a module when only import package
I wrote module1 in package1, and want to use a method named 'method1' in module1, the caller(test.py) is like this: import package1 package1.module1.method1() module1.py is like this: def method1(): print 'method1 run!' The filesystem structure is: test.py package1 |__ __init__.py |__ module1.py When i run test.py, the output is: AttributeError: 'module' object has no attribute 'module1' File "e:\MyDoc\GODU_BVT\test.py", line 2, in package1.module1.method1() If test.py is modified to: import package1.module1 ... then everything goes well. Python documentation said: "Contrarily, when using syntax like import item.subitem.subsubitem, each item except for the last must be a package; the last item can be a module or a package but can’t be a class or function or variable defined in the previous item." Obviously we can import a package besides a module, so i don't know why the "import package1" style does not work. Above codes tested on python 2.6.6, windows xp sp2. Thanks for explanations :) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to use a module when only import package
Steven wrote: leechau wrote: I wrote module1 in package1, and want to use a method named 'method1' in module1, the caller(test.py) is like this: import package1 package1.module1.method1() [...] When i run test.py, the output is: AttributeError: 'module' object has no attribute 'module1' File "e:\MyDoc\GODU_BVT\test.py", line 2, in package1.module1.method1() If test.py is modified to: import package1.module1 ... then everything goes well. Yes, that is correct, and that is a deliberate design. What if your package had a 1000 sub-modules, each of which were big? You wouldn't want loading the main package to automatically load all 1000 sub-modules, if you only needed 1. You either import the sub-module by hand: import package1.module1 and now you can use package1.module1.method1 (not really a method, actually a function). If you want module1 to automatically be available after importing the package, include one of these in the package1 __init__.py file: import module1 # should work in Python 2 and now package1 will include the module1 in its namespace. -- Steven Thanks for Steven's exellent and patient explanations. How should I do if automatically import a module in Python 3? Thanks again. -- leechau ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to design the structure of multi-steps(and substeps) scripts
Hi everybody. I wrote a script to run BVT and it's code structure like this: def step1: local_var1 = ... # some other variable definitions for step1 def substep11: pass def substep12: pass # more substeps def step2: local_var1 = ... # some other variable definitions for step2 def substep21: pass def substep22: pass # more substeps # more steps ... def stepN: #vars and substeps... global_var1 = ... # more global var definitions... if step1.return_success: step2 step3 ... stepN As you see, this script consists of serveral steps(a list of commands), and a step often consists of several substeps. I used to put all the steps in one module, where one step corresponding to one funciton and one substep corresponding to one nested function. It's now a headache to maintain this script because of totally 1000+ lines of code. Meanwhile when I manipulate different projects with this script I need to modify the value of some variables and detailed task logic. Obviously it's a dirty work to modify the source code directly for different projects. So I wonder if I should put different steps in different modules to shorten the length of each module? If I should put the default value of a variable and high level task logic in a abstract class, and define specific value and detailed task logic in concrete subclass? Is there any design patterns available for this kind of multi-steps scripts? Thanks! -- leechau ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor