uhuygiuhlk
uhuygiuhlk

Reputation: 23

Python 3 absolute import does not work

I have a folder with two files: test.py and csv.py . In test.py I have

import csv

This imports my csv.py file instead of importing the built-in csv module.

Why? I thought that absolute imports were default in Python 3?

How do I force Python to load the built-in csv module?

from __future__ import absolute_import

does not help. Neither does

csv = __import__('csv', level=0)

which, according to the docs, should "only perform absolute imports".

Renaming csv.py is not an acceptable solution. Also I'd rather not use the "from module import something" syntax (not that it helps in this case).

(Using Python 3.4.0 on Linux Mint 17.1)

Upvotes: 2

Views: 3036

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121176

You have a top-level csv module, which lives on the . path, so it is found before the built-in module. This is how absolute imports work.

Move your modules into a package if you expected csv to be 'local'. Move your modules into a directory with your package name, and add a __init__.py file (it can be empty). Your csv module is then namespaced as yourpackage.csv and won't be considered as a top-level module.

Upvotes: 5

Related Questions