[PyQt] modules, nameSpaces, and Wizards

Mike Ramirez gufymike at gmail.com
Thu Jan 6 01:47:11 GMT 2011


On Wednesday, January 05, 2011 10:12:30 am James Polk wrote:
> 
> So, strategically thinking,...what's the best way to achieve this kind
> of program/module sharing/linking?
> 
> Any thoughts, suggestions, advice, or words of wisdom, is mostly greatly
> appreciated,
> Thank you,
> -James
> 

I'm not really sure what your actual problem is, but it does sound like it's 
more of a python issue than a pyqt issue. (as the import statement is part of 
python, not pyqt).

But to give you some advice/words of wisdom, unless it's a one-off or similar 
application. I create a deep directory structure that seperates everything. 
Then my import problems are usually a path issue over anything else. In this 
situation it's a problem with relative imports, so most of the time I use 
aboslute imports. 

This is what my structure typically looks like now:

src/
src/__init__.py
src/main.py
src/lib/
src/lib/__init__.py
src/lib/widgets/__init__.py
src/lib/widgets/misc.py
src/lib/widgets/wizard
src/lib/widgets/wizard/__init__.py
src/lib/widgets/wizard/page1.py
src/lib/widgets/wizard/page2.py
src/lib/dialogs/__init__.py
src/lib/dialogs/wizard.py
src/lib/supportfunctions.py
....

(sometimes, if the support stuff is rather large, I'll seperate it a bit 
different having a qt and app directory to seperate things further, but at 
this point it's more of an organization for me.)

the src directory is added to the pythonpath.

In my files I import things like this:

in widgets/wizard/page2.py:

from PyQt4 import QtGui as QTG
from lib.widgets import misc as Misc

from lib.supportfunctions import myNeededFunc

class Page2(QTG.QWizardPage):
	def __init__(self, parent=None)
		super(Page2, self).__init__(parent)
		self.nameFrame = Misc.NameFrame()
		...
	def validatePage(self):
		# grab info taken from page
		name = self.nameFrame.name.text()
		...
		if myNeededFunc(name):
			return True
		return False
		
in dialogs/wizard.py:

from PyQt4 import QtGui a QTG
from lib.widgets import wizard as WizPages

class MyWizard(QTG.QWizard):
	def __init__(self, parent = None):
		super(MyWizard, self).__init__(parent)
		self.addPage(WizPages.Page1)
		self.addPage(WizPages.Page2)

		...

in main.py:

from PyQt4 import QtGui a QTG
from lib.dialogs.wizard import MyWizard
import sys
# any setup for the app is done here

# create an entry point to the app.
def main():
	app = QTG.QApplication(sys.argv)
	w = MyWizard()
	w.show()
	sys.exit(app.exec_())

if __name__ == "__main__":
	main()



NameFrame is an extended QFrame that contains two labels and a lineedit.

myNeededFunc does the heavy lifting of the page validation (similar to your 
stripped_main_AAA.py functions.).

Page1 is just a quick intro (straight text explaining the magic the wizard 
does for the user)

The entry point is used for things like setuputils and allowing it to create a 
script for me to have in /usr/bin and so on. I could have easily just left out 
main() and put the meat in the  if __name__ == "__main__" block. 

Anyways, in this situation, all my imports will normally work. The main cause 
of problems would be problems with the .py files I'm importing or path 
problems(as mentioned above).

Now to explain my problem with relative paths. it's that '.' ie:
	 from .widgets.misc import NameFrame

the leading . just doesn't play well when I'm testing packages from within the 
package dir. in this case if I run a test with the wd as lib/widgets, I'll get 
a "'.' is not a package" error, I need to run it from the lib dir. (Note: this 
is a py3 problem, py2 can use relative paths like this but you need to call 
from __future__ import absolute_import before any import statements.).

Now if all this is all good, then there is a 70% chance the file I'm importing 
has errors or 25% I'm using it wrong, the last 5% is the WTF is wrong part.


Here are the relative docs you might find useful:

Information on python modules, specifically packages:
http://docs.python.org/tutorial/modules.html#packages

the import statement:
http://docs.python.org/reference/simple_stmts.html#the-import-statement

the future statement:
http://docs.python.org/library/__future__.html


If all this already applies to you and doesn't help and you're still stuck, 
please send some actual error messages with relative code, they would be more 
informative than just trying to explain the issue.

HTH,

Mike




-- 
Hain't we got all the fools in town on our side?  And hain't that a big
enough majority in any town?
		-- Mark Twain, "Huckleberry Finn"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110105/417eab0e/attachment-0001.html>


More information about the PyQt mailing list