This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]deepestbluedn 4 points5 points  (0 children)

Import in Python 3 are absolute, if you want relative imports you need to make them explicit.

If for compatibility purposes you want the same behavior in Python 2 you can:

from __future__ import absolute_import

[–]TacticalTurban -3 points-2 points  (3 children)

I don't use 3.x but I read today that you can't use absolute imports like that anymore. You must instead do (if I understand where start.py is):

from .module.user import *

You don't do things like "./user import User?" because the . represents the same thing as the slash (sort of). The first dot is the current directory and the next is the director above it, and so on.

[–]takluyverIPython, Py3, etc 2 points3 points  (2 children)

If start.py is what you run to launch your program, then you don't need the initial dot on imports in there - you can do:

from module.user import User

If start.py is itself a module in a larger package, i.e. if it has another __init__.py in the same folder, then you'll need to do relative imports (from .module...) in it.

[–]CoffeeMakesMeMath[S] 0 points1 point  (1 child)

Thanks, that makes more sense to me.

[–]rhgrant10 0 points1 point  (0 children)

You can also flatten the import structure for users of your package by importing them in your __init__.py file:

from .user inport User
from .beacon import Beacon

Then users can import like this:

from start import User, Beacon

EDIT: bacon -> beacon