Python global variable problem

Started by
10 comments, last by King Mir 11 years, 9 months ago
I mean removing the __init__.py from the blobs folder and replacing


#Test/Test3.py
import Blobs.Test2

def func():
print Blobs.Test2.getVal() #blobs is top level package


by


#Test/Test3.py
import Test2

def func():
print Test2.getVal() #blobs is not a top level package anymore, this is just the project dir


When writing a python application, I usually follow this layout:


ProjectDir/
main.py
config.py
package1/
__init__.py
file1.py
file2.py
package2/
__init__.py
file1.py
file2.py


As package1 and package2 directories contains a __init__.py, I can do the following in the main script:


# main.py
import package1.file1
import package1.file2
import package2.file1
import package2.file2
def test():
package1.file1.a_func()
package1.file2.a_func()
package2.file1.a_func()
package2.file2.a_func()


And nothing prevents me to use the configuration module from the package1.file1 module:


import config
"""
no need to specify path as config.py sits in the same directory as the main.py (path is added to
sys.path automatically when the main script is started so python knows about all other files on the same level as the main script)
"""
def a_func():
print(config.a_value)


Edit: clarified last docstring + a typo
Advertisement
Thanks, I'll try that.

This topic is closed to new replies.

Advertisement