54

I'm new to Python and programming in general (a couple of weeks at most).

Concerning Python and using modules, I realise that functions can imported using from a import *.

So instead of typing

a.sayHi()
a.sayBye()

I can say

sayHi()
sayBye()

which I find simplifies things a great deal. Now, say I have a bunch of variables that I want to use across modules and I have them all defined in one python module. How can I, using a similar method as mentioned above or an equally simple one, import these variables. I don't want to use import a and then be required to prefix all my variables with a..

The following situation would by ideal:

a.py

name = "Michael"
age = 15

b.py

some_function

if name == "Michael":
    if age == 15:
        print("Simple!") 

Output:

Simple!
5
  • 12
    The "simplifies things a great deal" will turn out to be a very bad idea in the long run. When you have more than one or two modules, this "from module import *" doesn't work out all that well.
    – S.Lott
    Commented Jul 5, 2009 at 23:36
  • Yeah, I suppose. It's a useful trick anyway, where necessary.
    – user133446
    Commented Jul 6, 2009 at 10:25
  • 2
    It is mostly meant for edge cases, such as when you have a "base module" and several platform specific versions that might get imported, which want to grab the contents of the base and add to it. the os module works this way, doing 'from nt import *' or 'from posix import *' depending on the platform, for example.
    – ironfroggy
    Commented Jul 6, 2009 at 10:40
  • 2
    @Mr. November: Actually, it's not a useful trick. It's handy when you're in a classroom situation for days 1 and 2 of intro to Python. By day 3, it's no longer acceptable since it causes more problems than it solves.
    – S.Lott
    Commented Jul 6, 2009 at 12:32
  • I now see how "from module import *" could be a problem in most program situations, but I've found one particularly handy use for it that doesn't cause issues. Then again, I'm a Python newb so it could turn out to be altogether obsolete.
    – user133446
    Commented Jul 6, 2009 at 21:47

4 Answers 4

85

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!
3
  • 10
    WHAT?? Plenty of 'em! How about having to declare global name inside a function if that function wants to change the value of name?
    – bobobobo
    Commented Jan 3, 2010 at 16:57
  • 1
    I believe this does NOT import protected variables, such as _this_one.
    – mareoraft
    Commented May 23, 2016 at 22:21
  • 2
    This gives me the warning: "from module import *" used unable to detect undefined names. Commented May 10, 2020 at 18:05
39

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.

5
  • Thanks. I've never heard of from..import..as before.
    – user133446
    Commented Jul 7, 2009 at 9:24
  • 1
    If the answer was helpful to you, feel free to up-vote it! (At least, that's what the tool-tip says. :-)
    – cdleary
    Commented Jul 7, 2009 at 9:51
  • 1
    It was helpful, but I don't have 15 reputation yet and I need that much to vote ;_; If only I could be useful around here...
    – user133446
    Commented Jul 8, 2009 at 10:24
  • Ah, I forgot about that. No worries -- I'm sure you'll find some more good questions to ask/answer. :-)
    – cdleary
    Commented Jul 9, 2009 at 1:12
  • as @cdleary said importing * is not a very good idea, but if you want all the modules from the library, another alternative can be importin the library itself (e.g. import a) and then use the modules like (a.get_whatever())
    – Amyth
    Commented Dec 9, 2012 at 10:18
14

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.

0
8

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.