【问题标题】:python: import function doesnt work in 3.5python:导入功能在 3.5 中不起作用
【发布时间】:2018-07-30 05:19:54
【问题描述】:

我用它在 2.7 中导入一个有效的函数:

def import_x():
    import os
    import imp
    directory = os.path.dirname(os.path.realpath(__file__))
    os.environ['__PYTHON_SYS_PATH_FOR_TESTFOLDER__'] = directory
    fp, pathname, description = imp.find_module('test', [ directory ])
    if fp is not None:
        try:
            x = imp.load_module('test', fp, os.path.realpath(pathname), description)
        finally:
            fp.close()
        return x

test = import_x()

但我很难在 3.5 中完成这项工作。请问有什么办法吗?

【问题讨论】:

    标签: python python-2.7 python-3.5 python-importlib imp


    【解决方案1】:

    试试这个:

    import sys
    directory = os.path.dirname(os.path.abspath(sys.argv[0]))
    

    我不确定具体的更改,但这似乎有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-15
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多