与本地安装的pip的python代码工作方式不同

时间:2019-02-08 19:26:15

标签: python import pip

这是mad_import.py

import pieqt
import sys
import importlib
def mad_import(module):
    #for module in modules:
    libs = [m[0] for m in filter(lambda a: type(a[1]) == type(module), module.__dict__.items())]
    print(libs)
    for lib in libs:
        __import__(lib)
    generate_runtime_python_imports(libs,module)
    generate_runtime_python_imports_from(libs)
    importlib.import_module('runtime_python_imports_from')
    # __import__('runtime_python_imports_from.py')
    #return libs



def generate_runtime_python_imports(libs,module):
    dic = module.__dict__.items()
    moduleNames = []
    roukzouk = []
    for key in dic:
        for lib in libs:
            print(key[1])
            if key[1]!=None and lib in str(key[1]) and 'from' not in str(key[1]):
                print(True)
                roukzouk.append(str(key[1]))
                # sys.exit()
            if key[1] != None:
                d=str(key[1])+'s'
            moduleNames.append(d)

    print('-'*100)
    import_line_text = ""
    for import_line in roukzouk:
        for lib in libs:
            import_line = import_line[import_line.find(lib):].rstrip("'>")
            print(import_line)
            import_line_text+="import "+import_line+"\n"
    with open("runtime_python_imports.py", "w") as runtime_python_imports:
        runtime_python_imports.write(import_line_text)

def generate_runtime_python_imports_from(libs):
    import_line_from_text = ""
    for lib in libs:
        import_line_from_text += "from "+lib+" import *\n"
    with open("runtime_python_imports_from.py", "w") as runtime_python_imports_from:
        runtime_python_imports_from.write(import_line_from_text)

# import importlib
# for module in moduleNames:
#   if module not in {'__name__','__doc__','__package__','__loader__','__spec__','__file__','__cached__','__builtins__'}:
#       importlib.import_module(module)
# print(QApplication)

# mad_import(pieqt)

这是pieqt.py

import PySide2
import PySide2.QtCore
from PySide2.QtGui import*
from PySide2.QtWidgets import*

QApplication = PySide2.QtWidgets.QApplication
QTextEdit = PySide2.QtWidgets.QTextEdit
QLabel = PySide2.QtWidgets.QLabel
QPushButton = PySide2.QtWidgets.QPushButton
QCheckBox = PySide2.QtWidgets.QCheckBox
QMainWindow = PySide2.QtWidgets.QMainWindow
QVBoxLayout = PySide2.QtWidgets.QVBoxLayout
QWidget = PySide2.QtWidgets.QWidget

def user_function():
    print("I am a user function")
callbacks = []
def run():
    global callbacks
    for callback in callbacks:
        callback()
def callback_user_functions():
    global callbacks
    for callback in callbacks:
        callback()
def add_function(function):
    global callbacks
    callbacks.append(function)
def main():
    print("Enter your code below!!!")
    global callbacks
    add_function(user_function)
    run()
    #callback_user_functions()

if __name__ == "__main__":
    main()

这是test_mad_import.py

import pieqt
from pieqt import *
import mad_import

mad_import.mad_import(pieqt)
print(QTableView)

当我在该文件夹中运行test_mad_import.py时,它可以正常工作(我可以调用mad_import.mad_import(pieqt)并且存在QTableView)

但是当我转到另一个目录进行测试时,我会点点安装我的软件包(该软件包已在GitHub上载,并使用了twine),但无法调用mad_import.mad_import(pieqt)。 它给出TypeError:'模块'对象是不可调用的(如果我这样做mad_import(pieqt)) 而且AttributeError:模块'foo'没有属性'foo'(当我这样做mad_import.mad_import(pieqt))

与在文件夹中本地存储所有文件相比,pip安装软件有何区别?

0 个答案:

没有答案