Python - 导入全局/ site-packages模块,而不是本地目录中相同名称的文件 [英] Python - Importing a global/site-packages module rather than the file of the same name in the local directory

查看:653
本文介绍了Python - 导入全局/ site-packages模块,而不是本地目录中相同名称的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是python和virtualenv / pip。我有一个模块通过点号$ code> test_utils (它是django-test-utils)安装。在我的一个django应用程序中,我想导入该模块。但是,我还在同一目录中有另一个文件 test_utils.py 。如果我去 import test_utils ,那么它将导入这个本地文件。



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

解决方案

由于我的 test_utils 在django项目中,我可以从..test_utils import ... 导入全局。


I'm using python and virtualenv/pip. I have a module installed via pip called test_utils (it's django-test-utils). Inside one of my django apps, I want to import that module. However I also have another file test_utils.py in the same directory. If I go import test_utils, then it will import this local file.

Is it possible to make python use a non-local / non-relative / global import? I suppose I can just rename my test_utils.py, but I'm curious.

解决方案

Since my test_utils was in a django project, I was able to go from ..test_utils import ... to import the global one.

这篇关于Python - 导入全局/ site-packages模块,而不是本地目录中相同名称的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆