Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 correct way to import relative or absolute?

I am writing a python module neuralnet. It was working all fine in Python2, but in Python3 imports are failing.

This is my code structure.

neuralnet/
    __init__.py
    train.py         # A wrapper to train (does not define new things)
    neuralnet.py     # Defines the workhorse class neuralnet
    layer/
        __init__.py
        inlayer.py       # Defines input layer class
        hiddenlayer.py

application/         # A seperate application (not part of the package)
   classify.py       # Imports the neuralnet class from neuralnet.py

train.py needs to import neuralnet.py's neuralnet class.

neuralnet.py needs to import layers/inlayer.py etc.

(I prefer relative imports.)

I have a different application (classify.py) which needs to import this module. Where I do...

from neuralnet.neuralnet import neuralnet

I have tried a few ways to import. Either I get an error (mostly arcane like parent is not imported)

  1. While running train.py (which is a part of the neuralnet module)

    from . import layer  # In file neuralnet.py
    SystemError: Parent module '' not loaded, cannot perform relative import
    

Or

  1. while running classify.py (which is outside the module).

    from layer.inlayers import input_layer   # In file neuralnet.py
    ImportError: No module named 'layer'
    

My imports worked perfectly well for years in Python2. I am wondering what Python3 expects of me? Should I move train.py to outside my module (technically it is not a part of the module)? Please suggest best practice.

like image 626
rakesh a Avatar asked Feb 08 '15 23:02

rakesh a


People also ask

Should I use relative or absolute imports Python?

With your new skills, you can confidently import packages and modules from the Python standard library, third party packages, and your own local packages. Remember that you should generally opt for absolute imports over relative ones, unless the path is complex and would make the statement too long.

Should you use relative imports Python?

Because there, they want to make sure that other developers could also get the full path of the import module. Relative imports are helpful when you are working alone on a Python project, or the module is in the same directory where you are importing the module.

How should Python imports be ordered?

Imports should be grouped in the following order: standard library imports. related third party imports. local application/library specific imports.

How do I import absolute value in Python?

Absolute imports in PythonAbsolute import involves a full path i.e., from the project's root folder to the desired module. An absolute import state that the resource is to be imported using its full path from the project's root folder.


1 Answers

In Python 3, implicit relative imports are forbidden, see https://www.python.org/dev/peps/pep-0328/ and https://docs.python.org/release/3.0.1/whatsnew/3.0.html#removed-syntax:

The only acceptable syntax for relative imports is from .[module] import name. All import forms not starting with . are interpreted as absolute imports. (PEP 0328)

from .stuff import Stuff is an explicit relative import, which you "should" make use of whenever possible, and must use in Python 3, whenever possible. Head over to https://stackoverflow.com/a/12173406/145400 for a deeper analysis on relative imports.

like image 69
Dr. Jan-Philip Gehrcke Avatar answered Oct 05 '22 04:10

Dr. Jan-Philip Gehrcke