2

I have the main script file, let's call it main.py and I have module file which located in subdirectory "module/", let's call it mod.py (module/mod.py)

The directories tree is like this:

-main.py

-module/
 |
 |---mod.py

Now I have a global variable in main.py, call it "a" and it's equal to 1. I have a function in mod.py which should print the global variable a.

main.py:

import sys
sys.path.append("module/")
from mod import *

global a
a = 1

print_it()

module/mod.py:

from main import *

def print_it():
    global a
    print a

When I start the main.py script, I get:

NameError: name 'print_it' is not defined

Why?

0xPwn
  • 365
  • 2
  • 5
  • 13

3 Answers3

1

For the import part: The directories tree should look like this:

-main.py

-module/
 |
 |---__init__.py
 |---mod.py

The __init__.py file can be empty and it is not necessary in 3.x And if you follow the method to use global as below, you can import your method like that.

from mod import print_it

Instead of using a global variable, you can create a new module Common/Global (whatever you want to name it) as follow:

class Common:
    pass

common = Common()
common.a = 0

and you import it in your module

from Common import common

and you will have access to common.a in all your module and doesn't pollute your namespace with global variables.


Here an example:

Common.py as defined above.

mod.py as follow:

from Common import common

def printa():
    print common.a

and main.py:

from Common import common
from mod import printa

printa()
common.a = 10
printa()
romain-aga
  • 1,441
  • 9
  • 14
  • @Tadhg McDonald-Jensen: It does, with this you can define it with common.a in your main then use it in print_it, that is contained in another module without knowing if the variable in defined or not at first. – romain-aga May 19 '16 at 10:07
  • ok... but if it is not defined then you run into problems, and it still doesn't address the issue of `print_a` not being defined. – Tadhg McDonald-Jensen May 19 '16 at 10:23
  • _it still doesn't address the issue of `print_a` **the function** not being defined._ – Tadhg McDonald-Jensen May 19 '16 at 10:29
  • I'm still waiting for the OP to answer which version of python he uses, it is not the same import system between 2.x and 3.x – romain-aga May 19 '16 at 10:31
  • [this part](http://stackoverflow.com/questions/37319233/importing-modules-in-python-from-different-folders/37319931?noredirect=1#comment62158293_37319233) of it is the exact same as far as I am concerned. Anyway I agree that some common ground is a good idea hence my upvote, but I really think you could improve this by at least addressing the issue the OP is having instead of simply suggesting a completely different alternative. – Tadhg McDonald-Jensen May 19 '16 at 10:35
  • I already asked the question and I am computing the difference between the 2 version, like that the OP could chose. Thank for your feedback and upvote otherwise. – romain-aga May 19 '16 at 10:40
0

Try Giving the complete path of the module folder. If you are running it on a remote machine you can make use of os Module to get the absolute path.

import sys
from os import path as expath
sys.path.append(os.path.abspath(r'module/'))

Hope it helps. Happy Coding :)

Strik3r
  • 1,052
  • 8
  • 15
0

Basically what is happening is that main.py is importing everything from mod.py. At that moment python will execute line after line from mod.py but the first line of mod.py is importing everything from main.py again. So now python will execute line for line from main.py again. Since mod.py has already been previously imported python will not go back again to mod.py and continue line for line until it hits print_it() which, at that point, has not yet been defined because mod.py has not yet been fully executed due to the import statement in mod.py.

This answer explains the same idea with some good examples.

Community
  • 1
  • 1
sowa
  • 1,249
  • 16
  • 29