Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can python do imports after I clear sys.path - Import precedence

I have a python module named Queue that conflicts with the default queue in python.

While trying to force the import of the default queue, I tried to simply clear sys.path.

I was of the understanding that the imports are looked up from sys.path. But Python still seems to be able to import modules after I clear syspath.

Explain this please!

In [26]: sys.path
Out[26]: []
In [27]: import datetime
In [28]: datetime
Out[28]: <module 'datetime' from '/usr/local/python2.7/lib/python2.7/lib-dynload/datetime.so'>
In [31]: import xyz.Queue
In [32]: xyz.Queue
Out[32]: <module 'xyz.Queue' from '/public/abc/def/ghi/xyz/Queue/__init__.pyc'>
In [33]: sys.path
Out[33]: []

Also How to import native module queue instead of Queue.

I know that refactoring Queue is the solution this problem deserves, but it not the one it needs right now.

like image 939
rtindru Avatar asked Feb 26 '15 05:02

rtindru


1 Answers

Add from __future__ import absolute_import as the first line in your file.

This will force all imports to be absolute rather then relative. So import Queue will import the standard module, to import a local module you'd use from . import foobar

like image 193
backtrack Avatar answered Sep 28 '22 14:09

backtrack