Re: Imports again...
On 08/04/2010 14:16, Alex Hall wrote: The above link is to a project. I am new to using multiple files in Python, and I have a lot of tangled imports where many files in the same folder are importing each other. When I tried to follow the manual to make some files into packages, it did not work. Can anyone explain why I am getting an import error in the above project, and/or how I can clean up the file structure and imports to avoid problems like this in the future? Thanks in advance for any help, and I apologize for the broken link the other day. I don't have the energy at the moment to install all the dependencies to make the thing run, but just glancing at the layout... you appear to have a slightly confused idea of how packages are used. The main arm directory has an __init__.py as does a modes subdirectory and a weather subdirectory of that. But neither of these has any code in them to be imported. And the arm directory appears to be the main application directory so making that into a package doesn't seem to serve any purpose. I'm sure there are better explanations around, but in short: packages are directories with Python modules in them and, specifically, one Python module __init__.py which is typically empty (but needn't be). They can be considered in different ways, but essentially are ways of grouping Python modules according to some idea of cohesion. They don't magically make tangled code untangled but they might offer a certain clarity where you'd otherwise have one big directory full of undifferentiated modules. A typical example of packages would be a system which wanted to output to csv, html and pdf using some standardised API. The various output modules might live inside an outputs subpackage so the main program could do from outputs import csv or whatever. As it happens, this example also shows that the outputs.csv module won't shadow the stdlib csv module -- also purists might argue that it's bad practice to name any module over a stdlib module. Creating a package in your case might help you -- I haven't really looked at your code enough to say for sure -- to break one big module into several more structured modules within a package. TJG -- http://mail.python.org/mailman/listinfo/python-list
Re: Imports again...
Hi, please post your traceback. I guess you have a recursive import. This can lead to strange exceptions (for example AttributeError) Thomas Alex Hall wrote: Hello all, once again: http://www.gateway2somewhere.com/sw/sw.zip The above link is to a project. I am new to using multiple files in Python, and I have a lot of tangled imports where many files in the same folder are importing each other. When I tried to follow the manual to make some files into packages, it did not work. Can anyone explain why I am getting an import error in the above project, and/or how I can clean up the file structure and imports to avoid problems like this in the future? Thanks in advance for any help, and I apologize for the broken link the other day. -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de -- http://mail.python.org/mailman/listinfo/python-list
Re: Imports again...
On 8 abr, 10:16, Alex Hall mehg...@gmail.com wrote: Hello all, once again:http://www.gateway2somewhere.com/sw/sw.zip The above link is to a project. I am new to using multiple files in Python, and I have a lot of tangled imports where many files in the same folder are importing each other. When I tried to follow the manual to make some files into packages, it did not work. Can anyone explain why I am getting an import error in the above project, and/or how I can clean up the file structure and imports to avoid problems like this in the future? Thanks in advance for any help, and I apologize for the broken link the other day. In addition to what Tim Golden has said (which appears to be based on another version of this project - I don't see the file structure he describes), I noticed that weather.py spawns a new thread when imported; don't do that: http://docs.python.org/library/threading.html#importing-in-threaded-code Also, you have some .pyw files with corresponding .pyc file. That's *very* strange. .pyw files are *not* modules, and Python won't import them. Don't use the .pyw extension except for your main application script (in cases when you don't want a console window to appear). Having foo.pyc and foo.pyw in the same directory, Python will always load the .pyc file, ignoring any changes in the .pyw source. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Imports again...
On 09/04/2010 15:19, Gabriel Genellina wrote: In addition to what Tim Golden has said (which appears to be based on another version of this project Just downloaded again, and there's definitely an empty package structure of the kind I described. (Altho' I certainly did have a few other versions lying around from previous questions by the OP). Also, you have some .pyw files with corresponding .pyc file. That's *very* strange. .pyw files are *not* modules, and Python won't import them. Ahem. dump Python 2.6.4rc2 (r264rc2:75501, Oct 18 2009, 22:41:58) [MSC v.1500 32 bit (I Type help, copyright, credits or license for more information. open (xxx.pyw, w).write (print ('hello')) import xxx hello /dump TJG -- http://mail.python.org/mailman/listinfo/python-list
Re: Imports again...
Okay, what you all say makes sense, and I am going to try the package thing again. The modes dir is from my last attempt, as is its weather subdir. I think I see what I did wrong, at least I hope I do. I will also remove the init file from the main dir. Yes, arm is the main directory of the program. Also, I will try to update things so that most imports happen from the dependencies folder, avoiding the need to install/copy so much to your local install of Python. Here is a traceback of the program as it is right now; this is from the exact same version as the .zip file contains. Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\Alexcd c:\python26 c:\Python26python.exe i:\arm\main.pyw Traceback (most recent call last): File i:\arm\main.pyw, line 3, in module import arm, network, weather, dict File i:\arm\arm.py, line 4, in module import config File i:\arm\config.py, line 4, in module from main import exitProgram File i:\arm\main.pyw, line 3, in module import arm, network, weather, dict File i:\arm\network.py, line 4, in module arm.ready() AttributeError: 'module' object has no attribute 'ready' c:\Python26 I realize it may be odd to import from main.pyw, but I do not think that could be causing the problem... could it? Perhaps I should erase all the .pyc files and let it compile again, or would that not do anything? On 4/9/10, Tim Golden m...@timgolden.me.uk wrote: On 09/04/2010 15:19, Gabriel Genellina wrote: In addition to what Tim Golden has said (which appears to be based on another version of this project Just downloaded again, and there's definitely an empty package structure of the kind I described. (Altho' I certainly did have a few other versions lying around from previous questions by the OP). Also, you have some .pyw files with corresponding .pyc file. That's *very* strange. .pyw files are *not* modules, and Python won't import them. Ahem. dump Python 2.6.4rc2 (r264rc2:75501, Oct 18 2009, 22:41:58) [MSC v.1500 32 bit (I Type help, copyright, credits or license for more information. open (xxx.pyw, w).write (print ('hello')) import xxx hello /dump TJG -- http://mail.python.org/mailman/listinfo/python-list -- Have a great day, Alex (msg sent from GMail website) mehg...@gmail.com; http://www.facebook.com/mehgcap -- http://mail.python.org/mailman/listinfo/python-list
Re: Imports again...
Tim Golden wrote: On 09/04/2010 15:19, Gabriel Genellina wrote: In addition to what Tim Golden has said (which appears to be based on another version of this project Just downloaded again, and there's definitely an empty package structure of the kind I described. (Altho' I certainly did have a few other versions lying around from previous questions by the OP). Also, you have some .pyw files with corresponding .pyc file. That's *very* strange. .pyw files are *not* modules, and Python won't import them. Ahem. dump Python 2.6.4rc2 (r264rc2:75501, Oct 18 2009, 22:41:58) [MSC v.1500 32 bit (I Type help, copyright, credits or license for more information. open (xxx.pyw, w).write (print ('hello')) import xxx hello /dump TJG Good point. Just to be clear, if there are *both* .py .pyw... dump Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32 Type help, copyright, credits or license for more information. -- open (xxx.pyw, w).write (print ('hello')) -- open (xxx.py, w).write (print ('good-bye')) -- import xxx good-bye /dump ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: Imports again...
En Fri, 09 Apr 2010 11:29:50 -0300, Tim Golden m...@timgolden.me.uk escribió: On 09/04/2010 15:19, Gabriel Genellina wrote: In addition to what Tim Golden has said (which appears to be based on another version of this project Just downloaded again, and there's definitely an empty package structure of the kind I described. (Altho' I certainly did have a few other versions lying around from previous questions by the OP). Indeed! (Where did I look...?) Also, you have some .pyw files with corresponding .pyc file. That's *very* strange. .pyw files are *not* modules, and Python won't import them. open (xxx.pyw, w).write (print ('hello')) import xxx hello Oops! Last time I checked, it didn't work... but it was a very long time ago, I presume. pyw files are importable since Python 2.2! Thanks for the correction. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Imports again...
En Fri, 09 Apr 2010 13:10:44 -0300, Alex Hall mehg...@gmail.com escribió: c:\Python26python.exe i:\arm\main.pyw Traceback (most recent call last): File i:\arm\main.pyw, line 3, in module import arm, network, weather, dict File i:\arm\arm.py, line 4, in module import config File i:\arm\config.py, line 4, in module from main import exitProgram File i:\arm\main.pyw, line 3, in module import arm, network, weather, dict File i:\arm\network.py, line 4, in module arm.ready() AttributeError: 'module' object has no attribute 'ready' I realize it may be odd to import from main.pyw, but I do not think that could be causing the problem... could it? Yes, it *is* a problem. Note the traceback sequence: main imports arm, arm imports config, config imports arm *again* (which is only partially initialized), arm imports network, and network tries to use arm.ready and fails. Try to organize your modules hierarchically, so modules higher in the hierarchy may import (and use) other modules lower in the hierarchy, but not the other way around. Doing it that way helps also to make clear the intent of each module (and class). The 'main' script should be at the top of the hierarchy: 'main' may import anything, but no one may import 'main'. Put your high-level modules below it; they may use other low-level ones. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: imports again
En Tue, 06 Apr 2010 14:25:38 -0300, Alex Hall mehg...@gmail.com escribió: Sorry this is a forward (long story involving a braille notetaker's bad copy/paste and GMail's annoying mobile site). Basically, I am getting errors when I run the project at http://www.gateway2somewhere.com/sw.zip Error 404 -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list