1

I have a Utilities module which defines a few functions which are repeatedly used and am also adding in some constants. I'm running into trouble importing these constants though...

Let's say I'm working in class A, and I have a class in my constants also named A

from Utils.Constants import A as DistinctA
class A(object):
 .... Implementation ....
 some_var = DistinctA.SOME_CONSTANT
class Utils(object):
 class Constants(object):
 class A(object):
 SOME_CONSTANT = "Constant"

I'm probably making this too much like Java, so if so just yell / smack my knuckles with a ruler.

When I attempt to import that class, I get an error that there is no module named Constants. What's this python newbie missing?

asked Dec 29, 2010 at 17:00

1 Answer 1

3

The identifier after 'from' must point to a module; you can't refer to a class. While I'm not qualified to say whether your nested classes are 'pythonic', I have never seen it done like that before. I'd be more inclined to create a constants.py module that contains the A class. Then you could do this:

from constants import A as DistinctA

If you really want those constants to live inside utils, you could make utils a package:

utils/
utils/__init__.py
utils/constants.py

Then you can do:

from utils.constants import A as DistinctA
answered Dec 29, 2010 at 17:07
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.