Reputation: 16234
I am trying out the answer in this.
The __init__.py
file in a folder named MyLibs
contains:
LogFile = r'D:\temp'
In utils.py
in the same folder MyLibs
, I tried various ways to access the LogFile
variable:
from __init__ import *
print LogFile #Gives: NameError: name 'LogFile' is not defined`:
and:
import __init__
print MyLibs.LogFile #Gives: NameError: name 'MyLibs' is not defined
I got the errors while executing from MyLibs.utils import *
What is the fix I must do? I prefer a method where I can reference LogFile
directly without having to add namespace prefixes.
Upvotes: 0
Views: 181
Reputation: 16234
My mistake.
The updated __init__.py
was somehow not executed. I started a new Python session and it worked.
Sorry for the false alarm.
Upvotes: 1
Reputation: 30151
Not sure how to do this with 2.7's implicit relative imports, but you could try this:
from __future__ import absolute_import
from . import LogFile
If you're running the Python module directly, you need to run it with python -m MyLibs.utils
rather than python MyLibs/utils.py
.
Upvotes: 0