2

I have a module in my PYTHONPATH that looks like this:

module.py

from __future__ import absolute_import

import foo

foo.do_something()

print("I am a module!")

I later call this script using python -m module. The script works most of the time but I found that I'm cd'ed into a directory which has a folder with foo/__init__.py inside of it, Python prefers that folder over the other foo module in my PYTHONPATH. I assume it's because Python is treating the foo folder as an implicit, relative import (I'm using Python 2.7).

How do I get Python to not treat the folder in the $PWD as an implicit relative import?

from __future__ import absolute_import does not work

module.py and every other module I use has from __future__ import absolute_import included. So existing questions such as Python: Disabling relative import do not solve this issue.

ColinKennedy
  • 828
  • 7
  • 24

1 Answers1

3

You can remove the current directory from sys.path, or put the directory of the foo module at the top of sys.path

# at the very beginning of module.py
import sys

sys.path = sys.path[1:]
# or
sys.path.insert(0, <directory of foo module>)

# your code ...
from __future__ import absolute_import

import foo

foo.do_something()

print("I am a module!")
Panwen Wang
  • 3,573
  • 1
  • 18
  • 39