[英]How to expand os.path to include submodules
I made a simple code editor using PyQt4. 我使用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. 我尝试在编辑器中扩展
os.path
,使其表现得像从Windows运行脚本海峡。
the other solutions I read here an stackoverflow. 我在这里阅读的其他解决方案是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? 如何在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 显示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
. 这些
PATH
env var中的所有文件夹添加到模块搜索路径中(我不确定这是一个好主意,因为那里有很多不包含或不应该包含python模块的文件夹)+用户的HOME
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.