10

I have 2 files main.py and irc.py.
main.py

import irc
var = 1
func()

irc.py

def func():
    print var

When I try to run main.py I'm getting this error

NameError: global name 'var' is not defined

How to make it work?

@Edit
I thought there is a better solution but unfortunately the only one i found is to make another file and import it to both files
main.py

import irc
import another
another.var = 1
irc.func()

irc.py

import another
def func():
    print another.var

another.py

var = 0

5 Answers 5

11

Don't. Pass it in. Try and keep your code as decoupled as possible: one module should not rely on the inner workings of the other. Instead, try and expose as little as possible. In this way, you'll protect yourself from having to change the world every time you want to make things behave a little different.

main.py

import irc
var = 1
func(var)

irc.py

def func(var):
    print var
Sign up to request clarification or add additional context in comments.

4 Comments

It would be easier for me to move this function to main file instead of adding this argument
@gr56: Then it sounds like refactoring is in order. If that's not possible either, I'd go with your solution of a third module which contains just the var object and is imported by both. If that works for you, please post it as an answer to your own question and accept it.
@SamirTalwar You can do this for functions, what about for instantiation of classes? For example, you want to define instances of a class in another module which you can import. The arguments can't be placeholders in this case.
It works for me only when I execute irc.py earlier (run script). I use Python 3 and Spyder. If not I get name func is not defined.
10

Well, that's my code which works fine:

func.py:

import __main__
def func():
    print(__main__.var)

main.py:

from func import func

var="It works!"
func()
var="Now it changes!"
func()

2 Comments

I like this solution and it works ... but eclipse gives me an 'Undefined variable from import: var' error on any imported variable or function from main. Any way of getting around this?
4

Two options.

from main import var

def func():
    print var

This will copy a reference to the original name to the importing module.

import main

def func():
    print main.var

This will let you use the variable from the other module, and allow you to change it if desired.

7 Comments

Oh, this is just wrong. Mutual imports give me the heebie jeebies.
Then you just don't know what you're doing. Mutual imports are perfectly safe, and work fine given certain very clear restrictions.
@Ignacio: And they'll work fine in this toy code designed to highlight the problem. I have doubts they'll work as expected in the real thing. Even if they do, they will encourage fuzzy lines between modules which serve only to make adding new features and maintaining the old ones so much more difficult than it has to be. I can't imagine a situation where mutual imports work better than a third module that serves the original two.
This would be the best option for me but it gives me an error: AttributeError: 'module' object has no attribute 'func'
@gr56: It should be __main__ instead of main
|
0

Well, var in the function isn't declared. You could pass it as an argument. main.py

import irc
var = 1
func(var)

irc.py

def func(str):
    print str

Comments

0

What Samir said works for Python 2

For python 3 you need to do it this way.

main.py

import irc
from irc import func

var = 1
func(var)
irc.py

irc.py

def func(var):
    print(var)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.