-1

Imagine a situation, in which you have two files say p1.py and p2.py.

p1.py:

a=5

p2.py:

from p1 import *

print(a) # NameError: name 'a' is not defined

print(p1.a) # NameError: name 'p1' is not defined

The first print statement is understandable. I am boggling over second print statement.

from p1 import * 

should import everything inside p1.py, so why am I not able to access p1.a variable. This is not the end, interesting part starts from here. Now consider the below-given modification:

import p1

print(p1.a) # prints a = 5

So what am I missing here? In this answer from @ThiefMaster he says that

from foo import * imports a to the local scope. When assigning a value to a, it is replaced with the new value - but the original foo.a variable is not touched.

So unless you import foo and modify foo.a, both calls will return the same value.

So I modified p1.a but why isn't it working?

Tom
  • 16,842
  • 17
  • 45
  • 54
Adarsh Maurya
  • 348
  • 1
  • 4
  • 14

2 Answers2

1

You wrote "from p1 import * should import everything inside p1.py, so why am I not able to access p1.a variable"

It does indeed import everything from inside the p1 module, but the module p1 does not contain any variable named p1, so after this import statement p1 is undefined. If instead you did import p1 then you could access p1.a1.

If as you claim, the module p1 contains a=5 then contrary to what you wrote your first print statement print(a) will succeed as from p1 import * creates a variable inside the main module with the same name and value as the variable a inside p1.

Duncan
  • 92,073
  • 11
  • 122
  • 156
0

These are the two way of importing a module.

  • import foo
  • from foo import foo1, foo2

What is a Module?

A module is a file containing Python definitions and statements. Within a module, you can access module's name using global variable __name__.

As you can see module contains Python definitions and statements which in short means executable statements such as functions, classes etc. They are used to initialize the module. And so, they will be executed the first time they are imported.

Each module has it's own PRIVATE SYMBOL TABLE which is used by all the functions and variables inside the module. This prevents accidental clashes of variables used by the author of module and user.

So in your case, p1.py and p2.py both will have its own private table where the list of all variables which can be used by the respective script. When you do something like this:

import p1

it creates a table in which module name is also introduced i.e. p1.

The other variant:

from p1 import *

does indeed creates a table, but it fails to import modules' name in its symbol table. In other words, p1 is missing. This includes everything in the table except modules' name and variable which starts with an underscore (_).

In most cases Python programmers do not use this facility since it introduces an unknown set of names into the interpreter, possibly hiding some things you have already defined.

Note that in general the practice of importing * from a module or package is frowned upon since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.

This may help.

Adarsh Maurya
  • 348
  • 1
  • 4
  • 14