0

I made a simple code editor using PyQt4.

Problem is when opening and executing a file that depends on a sub module, it cant find it locate them even when in same folder.

I try to expand os.path, in editor so it behaves like running a script strait from windows.

the other solutions I read here an stackoverflow. is only solutions if you would know what all the sub modules are called.

Import * include submodules

How to do relative imports in Python?

import os
import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from 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 the editor

import sys
print sys.path

shows the path variables from windows

Community
  • 1
  • 1
michael
  • 31
  • 7
  • _I try to expand `os.path`_...what do you mean by that? `os.path` is a _Python_ module. – CristiFati Jul 10 '15 at 09:06
  • yes i know. im trying to make the editor behave like when you run script strait from windows. When run from windows some python script it finds the submodules automatic. when using editor, and have open a python module script file, it cant not find the submodules that the script needs. so im trying to figure out how to do this. – michael Jul 10 '15 at 09:17
  • I think i see what you mean. When running the script from `cmd`, it probably finds its dependent modules because it knows where to search for them (most likely because `PYTHONPATH` is properly set), you probably could configure the interpreter that your editor uses, accordingly. – CristiFati Jul 10 '15 at 10:04
  • yes so are there a way to add the path to editor. I thought i did it with code os.path.join(os.path.expanduser('~'), os.path.expandvars('%Path%')) still i get the error no module named something – michael Jul 10 '15 at 10:10
  • Can you supply the command that works from `cmd`? The full command, the command output, and the working directory. – CristiFati Jul 10 '15 at 16:12

1 Answers1

0

I believe that what you meant by the first line after the imports is:

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

These lines add to the module search path all the folders in PATH env var (I'm not sure that this is such a good idea since there are lots of folders there who don't - or shouldn't - contain python modules) + the user's HOME.

CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • im trying to open for test a example file by module pygame. http://www.pygame.org/download.shtml – michael Jul 10 '15 at 12:08
  • im trying to open for test a example file by module pygame. http://www.pygame.org/download.shtml output NameError: global name 'pygame' is not defined so the app cant find the submodules in the example. – michael Jul 10 '15 at 12:17
  • Which is the file? and if you run from cmd `python pygamefile.py` it works? – CristiFati Jul 10 '15 at 14:01
  • Yes works fine via cmd.from the code editor it don't,this is the question I have been asking all along how to make the editor behave like this.remember this is a code editor so subprocess I can't use.I need a way to run to interpreter – michael Jul 10 '15 at 15:18
  • i think the problem is, when i open file into textbox and execute it.Its basiclly a ghost file. i think maybe i gotta save it somehow before exec. – michael Jul 13 '15 at 15:03