How python can import after cleaning sys.path - import priority

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

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

I understood that import is being viewed from sys.path. But Python can still import modules after clearing syspath.

Explain it 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]: [] 

Just like importing your own module queue instead of a queue.

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

+5
source share
1 answer

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

This will result in all imports being absolute rather than relative. Thus, import Queue imports a standard module to import a local module that you would use from . import foobar from . import foobar

+1
source

Source: https://habr.com/ru/post/1214215/


All Articles