Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How can I import all variables?

People also ask

Can you import all in Python?

The Python import statement imports code from one module into another program. You can import all the code from a module by specifying the import keyword followed by the module you want to import.

How do I import an entire module?

You need to use the import keyword along with the desired module name. When interpreter comes across an import statement, it imports the module to your current program. You can use the functions inside a module by using a dot(.) operator along with the module name.

How do you import all functions in Python?

We can use import * if we want to import everything from a file in our code. We have a file named functions.py that contains two functions square() and cube() . We can write from functions import * to import both functions in our code. We can then use both square() and cube() functions in our code.


You gave the solution yourself: from a import * will work just fine. Python does not differentiate between functions and variables in this respect.

>>> from a import *
>>> if name == "Michael" and age == 15:
...     print('Simple!')
...
Simple!

Just for some context, most linters will flag from module import * with a warning, because it's prone to namespace collisions that will cause headaches down the road.

Nobody has noted yet that, as an alternative, you can use the

from a import name, age

form and then use name and age directly (without the a. prefix). The from [module] import [identifiers] form is more future proof because you can easily see when one import will be overriding another.

Also note that "variables" aren't different from functions in Python in terms of how they're addressed -- every identifier like name or sayBye is pointing at some kind of object. The identifier name is pointing at a string object, sayBye is pointing at a function object, and age is pointing at an integer object. When you tell Python:

from a import name, age

you're saying "take those objects pointed at by name and age within module a and point at them in the current scope with the same identifiers".

Similarly, if you want to point at them with different identifiers on import, you can use the

from a import sayBye as bidFarewell

form. The same function object gets pointed at, except in the current scope the identifier pointing at it is bidFarewell whereas in module a the identifier pointing at it is sayBye.


Like others have said,

from module import *

will also import the modules variables.

However, you need to understand that you are not importing variables, just references to objects. Assigning something else to the imported names in the importing module won't affect the other modules.

Example: assume you have a module module.py containing the following code:

a= 1
b= 2

Then you have two other modules, mod1.py and mod2.py which both do the following:

from module import *

In each module, two names, a and b are created, pointing to the objects 1 and 2, respectively.

Now, if somewhere in mod1.py you assign something else to the global name a:

a= 3

the name a in module.py and the name a in mod2.py will still point to the object 1.

So from module import * will work if you want read-only globals, but it won't work if you want read-write globals. If the latter, you're better off just importing import module and then either getting the value (module.a) or setting the value (module.a= …) prefixed by the module.


You didn't say this directly, but I'm assuming you're having trouble with manipulating these global variables.

If you manipulate global variables from inside a function, you must declare them global

a = 10
def x():
   global a
   a = 15

print a
x()
print a

If you don't do that, then a = 15 will just create a local variable and assign it 15, while the global a stays 10