简体   繁体   中英

How to expand os.path to include submodules

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM