Python - 导入全局/站点包模块而不是本地目录中的同名文件

标签 python django import python-module

我正在使用 python 和 virtualenv/pip。我通过 pip 安装了一个名为 test_utils 的模块(它是 django-test-utils)。在我的一个 Django 应用程序中,我想导入该模块。但是,我在同一目录中还有另一个文件 test_utils.py。如果我执行 import test_utils,它将导入这个本地文件。

是否可以让 python 使用非本地/非相对/全局导入?我想我可以重命名我的 test_utils.py,但我很好奇。

最佳答案

您可以通过更改 sys.path 来切换搜索顺序:

del sys.path[0]
sys.path.append('')

这会将当前目录放在系统搜索路径之后,因此本地文件不会隐藏标准模块。

关于Python - 导入全局/站点包模块而不是本地目录中的同名文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5299199/

相关文章:

python - 步入 IPython 中的函数

python - 在不重复代码的情况下测试 Flask WTForms 验证器

python - 为什么 pytest 总是说 "ImportError: attempted relative import with no known parent package"

C#读取网站xml文件

python - 解释 Python 子进程模块中的示例管道

python - 如何使用 numpy 从源代码构建 pytorch

django - 当 SESSION_COOKIE_SECURE = True 时如何在 HTTP 中获取一些用户身份信息

50,000,000 个字符数据的 Django textarea

python - Django新手错误: - TypeError: view must be a callable or a list/tuple in the case of include()

php - 使用 CSV 文件导入文本 MYSQL