0

I have a package containing many .py files.

I want to:

  1. have a debug flag, which I can access in each file. (Actually write it in one file, read it in all other files)
  2. have a version number in each file, which is individual for each file. But I would like to create one function to get all version numbers of all files.

How do I define variables in these cases? How do I use import?

Edit: I use an empty __init__.py file and:

in package/classes.py:

debug = False

in package/db.py:

from package.classes import *

print debug

2

1 Answer 1

2

Suppose you have a variable named FLAG in your files/variables.py file.

If the files folder has an empty __init__.py file inside, you just need to do

from files.variables import *

and you can access it in every file you import it.

To go with your example:

folder structure:

\program\ 
         \package\
                  __init__.py
                  classes.py
                  db.py

in db.py

from .classes import *
print debug

should work

About the versioning, if you want to do it manually i think you have to do the same, but it's a hassle to mantain by hand the versions.

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

4 Comments

I tried: # in package/classes.py: debug = True # in init.py: from package.classes import * from package/db import * # in package/db.py: print debug What is wrong?
leave the init file empty, and eventually update the main post, not a comment, or it will be harder for us. the import statement should inside every file that's using it, not in the init actually
".classes" does not work. and it does not know debug in the db.py if I use "package.classes" either. :( I just hate importing logics...
i see just now you're using python2.4, so maybe the '.classes' isn't working because of that. maybe there's something else lying around you're not telling us? like this, everything should work smoothly

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.