Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to figure out where to define package-wide constants. #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

agartland
Copy link

@arokem
Currently this isn't working and I'm hoping you either know the right way to do this or can help me figure it out. The scenario is that I have a set of constants that should be defined package-wide and they should be importable by any module (or any module within a subpackage, which doesn't really apply here yet).

It sounds like one can either define them in __init__.py or a new module e.g. constants.py if you don't want to clutter your init. But I can't get either to work. Please let me know if you have ideas to try!

Thanks

@rbiswas4
Copy link

rbiswas4 commented Sep 4, 2015

@agartland . Interesting. There are three things one may think of when trying to do this, and I think you are asking how the 3rd is done (which I don't know how to do). I can set up the first 2:

  1. make CONSTANT1 available so that for any file in the package, we can access it as shablona.CONSTANT1. This also becomes available externally if shablona is imported
>>> import shablona
>>> print shablona.CONSTANT1
>>> 5 #what I set it to
  1. Make it so that CONSTANT1 is available as CONSTANT1 everywhere when shablona is imported. So, not only in shablona.py can we simply refer to CONSTANT1, but also when when we import shablona externally
import shablona
print CONSTANT1
  1. What I guess you would want is so that CONSTANT1 is available as CONSTANT1 within the module, so that in shablona.py, we can write
print CONSTANT1 # No imports necessary

as in (2), but externally

import shablona
print CONSTANT1
# Throws error

Forgot to say, I also put examples of how I usually would do it, but requires an import statement in the file you are importing to https://github.com/rbiswas4/shablona/tree/package_wide_constants_normal

and using builtins (and not requiring imports, but clogging up the external namespace) in https://github.com/rbiswas4/shablona/tree/package_wide_constants/shablona

@arokem
Copy link
Contributor

arokem commented Sep 5, 2015

Strange. This does seem to work for me (assuming I am understanding what you are trying to do here). See #5 for a test that passes on my machine (and hopefully also on Travis). If that misses the mark, maybe you could add a test that fails, to demonstrate what it is that you are trying to do?

@arokem
Copy link
Contributor

arokem commented Sep 5, 2015

I eat my words! That works on my machine, but fails on Travis. Let me see if I can make sense of this...

@arokem
Copy link
Contributor

arokem commented Sep 5, 2015

OK - looks like the solution might be to do this:

arokem@090947f

Is there some use-case that isn't covered by that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants