Accessing variables in __init__.py

G

Gaudha

my_package/
__init__.py
my_module1.py
my_module2.py
variables.py

I want to define common variables in __init__.py and use the namespace in my_module1.py or my_module2.py. Defining it is not a problem. How can call it from my modules?

If I define them in a module (say, variables.py), I can call them by importing variables.py in other modules. How can it be done if I define it in __init__.py?

It may be a silly query as I am newbie in Python. But, I would be grateful to get help.
 
M

Marco Nawijn

my_package/

__init__.py

my_module1.py

my_module2.py

variables.py



I want to define common variables in __init__.py and use the namespace in my_module1.py or my_module2.py. Defining it is not a problem. How can call it from my modules?



If I define them in a module (say, variables.py), I can call them by importing variables.py in other modules. How can it be done if I define it in __init__.py?



It may be a silly query as I am newbie in Python. But, I would be grateful to get help.

Hi,

If you store the variables in __init__.py, you can import them from the package. So in your case suppose __init__.py contains:
a = 10
b = {1 :"Hello", 2: "World" }

Than if you import my_package, you can access the variables as follows (interactive IPython session):

In [1]: import my_package

In [2]: my_pack
my_package my_package/

In [2]: my_package.
my_package.a my_package.b

In [2]: my_package.a
Out[2]: 10

In [3]: my_package.b
Out[3]: {1: 'Hello', 2: 'World'}

In [4]:
 
G

Gaudha

my_package/











I want to define common variables in __init__.py and use the namespace in my_module1.py or my_module2.py. Defining it is not a problem. How can call it from my modules?



If I define them in a module (say, variables.py), I can call them by importing variables.py in other modules. How can it be done if I define it in __init__.py?



It may be a silly query as I am newbie in Python. But, I would be grateful to get help.



Hi,



If you store the variables in __init__.py, you can import them from the package. So in your case suppose __init__.py contains:

a = 10

b = {1 :"Hello", 2: "World" }



Than if you import my_package, you can access the variables as follows (interactive IPython session):



In [1]: import my_package



In [2]: my_pack

my_package my_package/



In [2]: my_package.

my_package.a my_package.b



In [2]: my_package.a

Out[2]: 10



In [3]: my_package.b

Out[3]: {1: 'Hello', 2: 'World'}



In [4]:

Yea. I got it. It was a new information for me. A module in a package can import its own mother package to call the variables in __init__.

Is it funny or an extraordinary feature? Anyway. I felt it as something weird. Guido should have done it something like how 'self' behaves in classes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top