1

I have a package myapp with the following structure:

/myapp
  __init__.py
  User.py

Inside the User.py:

from flask_login import UserMixin
class User(UserMixin):

    def __init__(self, id):
      #more

To import it I have to use:

from myapp.User import User

Is it possible to self-define the user.py file as a class the same way you can in Java? And then import it: from myapp import User?

I guess that the only way to do is define the User inside the __init__.py. Is it so?

4
  • You can import User in __init__.py: from User import User and then from myapp import User should work fine. But what's wrong with the current way? Commented Aug 28, 2014 at 18:20
  • If user.py only defines a single class, then perhaps it can be merged with another module. Commented Aug 28, 2014 at 18:21
  • 1
    @AshwiniChaudhary don't use implicit relative imports. Either use an explicit relative import from .user import User (note the dot in .user) or use an absolute import from myapp.user import User. Using implicit relative import will break code when moving to different python versions and can even break code in other ways (e.g. problems with pickled data using the User class imported in the wrong way). Commented Aug 28, 2014 at 18:26
  • @Bakuriu Thanks for the info, my bad I missed a .. Commented Aug 28, 2014 at 18:36

2 Answers 2

1

When you write import myapp, you're really importing myapp/__init__.py's namespace into the local namespace under the name myapp. When you say: from myapp import something, you're taking something in the global namespace of myapp and bringing it into the local namespace.

SO, to have it the way you want, you just need to do this in __init__.py

# __init__.py
from myapp.User import User
# alternatively, using package local imports:
# from .User import User

Because now __init__.py holds a reference to the User class in it's global namespace and from there, that reference can be imported elsewhere.

Sign up to request clarification or add additional context in comments.

Comments

1

You can import your class inside __init__.py to make it directly available for importing through the package:

from .User import User

Be wary though that this masks the User module in this case where the module name equals the class name. This hinders users of your package from importing the module itself using a simple import statement.

They can still get to it through sys.modules (or importlib.import_module() for that matter) and import objects defined within the module using from … import … (e.g. from app.User import user_related_function will still work and import user_related_function from app.User [the module] despite app.User referring to app.User.User in your local namespace).

2 Comments

I'm not sure if it does mask the submodule making it un-importable... import package.module should still work I think ...
@mgilson It will definitely mask the module, but we can still use sys.modules['myapp.User'] to get the module itself. Something similar was asked earlier(I couldn't find it though)which pointed out the issue in github.com/sympy/sympy/blob/master/sympy/simplify/__init__.py file, here the module fu can now only be imported using sys.modules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.