1

I wanted to import a variable defined in function to another python file

e1.py

 def abc():
     global a
     a=10

e2.py

import e1
def defi():
    c=e1.abc.a
    print(c)

defi()   

I have searched but didn't get the right answer

Following is the error-

 Traceback (most recent call last):
 File "C:\Users\gkaur\Documents\MSO Editor Tool\e2.py", line 1, in <module>
 from e1 import abc
 File "C:\Users\gkaur\Documents\MSO Editor Tool\e1.py", line 2
 global a
 SyntaxError: name 'a' is parameter and global
7
  • If it's a global variable, then you should be able to import it directly from the file. It will not include the setting made in abc() because this is a runtime setting. Commented Nov 24, 2015 at 20:11
  • if importing the file gives you an error, generally trying to import anything from it will give you an error. You should fix the error. Also, using global variables is generally frowned upon... Commented Nov 24, 2015 at 20:12
  • I have tried that too.. just writing print a but it still gives an error Commented Nov 24, 2015 at 20:13
  • Well, whatever that error is, that's your first problem. Fix that. Commented Nov 24, 2015 at 20:14
  • 1
    There's no module called e1 shown. Are these files named "e1.py" and and "e2.py" or "file1.py"and "file2.py"? Also, abc has no attribute named "a". Commented Nov 24, 2015 at 20:18

3 Answers 3

1

You are not using the function in file 1 correctly it should be:

def abc():
    a = 10
    return a

You should be returning the value. for the second file it should be:

import e1
def defi():
    c = e1.abc()
    print(c)

defi()

without the () at the end of e1.abc() it doesn't actually tell the function to perform it's specific task.

Sign up to request clarification or add additional context in comments.

5 Comments

it still give an error Traceback (most recent call last): File "C:\Users\gkaur\Documents\MSO Editor Tool\e2.py", line 8, in <module> defi() File "C:\Users\gkaur\Documents\MSO Editor Tool\e2.py", line 4, in defi c=e1.a() AttributeError: 'module' object has no attribute 'a'
so there is no variable named a in your file then remove the line global a
this doesn't work too :( Traceback (most recent call last): File "C:\Users\gkaur\Documents\MSO Editor Tool\e2.py", line 8, in <module> defi() File "C:\Users\gkaur\Documents\MSO Editor Tool\e2.py", line 4, in defi c=e1.a() AttributeError: 'module' object has no attribute 'a' >>>
oh why are you doing e1.a() it's supposed to be the name of the function which is abc(), so it should be e1.abc() e1 is the file and abc() is the function
No problem. Have fun programming!
1

a is a module global variable in e1. Other than being set in abc, it is unrelated to that function; abc.a is an error.

import e1
def defi():
    c = e1.a
    print(c)

# This should produce an error
defi()   

However, unless you give a a value in the global scope of e1, it does not exist until you call abc.

import e1
def defi():
    c = e1.a
    print c

e1.abc()
defi()

1 Comment

Can you be more specific? What happens?
1

Your variable a is not defined until you call abc().

As you can see with dir(e1) or help(e1) in a repl, e1 does not have a variable a, only a function abc. Then after a call to abc(), a is here and set to 10.

>>> import e1
>>> dir(e1)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'abc']
>>> e1.abc()
>>> dir(e1)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'a', 'abc']
>>> e1.a
10

Comments

Your Answer

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