在这个问题中有一个答案(How to import a module given the full path?)。这是获得 400 票赞成票的第二个答案。然而,答案从未被编辑以包含许多人评论要求的内容。他在回答中说要这样做:'向 sys.path 添加路径(相对于使用 imp)的优势在于,它可以简化从单个包导入多个模块时的操作。例如:import sys# the mock-0.3.1 dir contains testcase.py, testutils.py & mock.pysys.path.append('/foo/bar/mock-0.3.1')from testcase import TestCasefrom testutils import RunTestsfrom mock import Mock, sentinel, patch'许多人问的基本上是我们如何使用sys.path.append指向单个 python 文件而不是目录?
添加回答
举报
0/150
提交
取消