python - How to expand os.path to include submodules -


i made simple code editor using pyqt4.

problem when opening , executing file depends on sub module, cant find locate them when in same folder.

i try expand os.path, in editor behaves running script strait windows.

the other solutions read here stackoverflow. solutions if know sub modules called.

import * include submodules

how relative imports in python?

import os import sys pyqt4 import qtcore, qtgui pyqt4.qtcore import * pyqt4.qtgui import *  os.path.join(os.path.expanduser('~'), os.path.expandvars('%path%'))   try:     _fromutf8 = qtcore.qstring.fromutf8 except attributeerror:     def _fromutf8(s):         return s  try:     _encoding = qtgui.qapplication.unicodeutf8     def _translate(context, text, disambig):         return qtgui.qapplication.translate(context, text, disambig, _encoding) except attributeerror:     def _translate(context, text, disambig):         return qtgui.qapplication.translate(context, text, disambig)  class ui_form(object): def setupui(self, form):     form.setobjectname(_fromutf8("form"))     form.resize(845, 848)     self.runbtr = qtgui.qpushbutton(form)     self.runbtr.setgeometry(qtcore.qrect(560, 670, 75, 23))     self.runbtr.setobjectname(_fromutf8("runbtr"))     self.openbtr = qtgui.qpushbutton(form)     self.openbtr.setgeometry(qtcore.qrect(680, 670, 75, 23))     self.openbtr.setobjectname(_fromutf8("openbtr"))     self.textedit = qtgui.qtextedit(form)     self.textedit.setgeometry(qtcore.qrect(30, 60, 701, 501))     self.textedit.setobjectname(_fromutf8("textedit"))      self.retranslateui(form)     qtcore.qmetaobject.connectslotsbyname(form)  def retranslateui(self, form):     form.setwindowtitle(_translate("form", "form", none))     self.runbtr.settext(_translate("form", "run", none))     self.openbtr.settext(_translate("form", "open", none))     self.runbtr.clicked.connect(self.runtext)     self.openbtr.clicked.connect(self.openfile)  def runtext(self):     exec str(self.textedit.toplaintext())      def openfile(self, path=none):     if not path:         path = qtgui.qfiledialog.getopenfilename(self.openbtr, "open file", '', "python files (*.py *.pyc *pyw)")      if path:         infile = qtcore.qfile(path)         if infile.open(qtcore.qfile.readonly | qtcore.qfile.text):             text = infile.readall()              try:                 # python v3.                 text = str(text, encoding='ascii')             except typeerror:                 # python v2.                 text = str(text)              self.textedit.settext(text)  if __name__ == "__main__":     import sys     app = qtgui.qapplication(sys.argv)     form = qtgui.qwidget()     ui = ui_form()     ui.setupui(form)     form.show()     sys.exit(app.exec_()) 

using editor

import sys print sys.path 

shows path variables windows

i believe meant first line after imports is:

sys.path.extend(os.path.expandvars("%path%").split(os.pathsep)) sys.path.append(os.path.expanduser("~")) 

these lines add module search path folders in path env var (i'm not sure such idea since there lots of folders there don't - or shouldn't - contain python modules) + user's home.


Comments