1

After doing research on this site and others, I create the topic. I have created a module like :

module.py

def function(var1, var2):
    ...
    return(var3)

And the main code is :

main.py :

import module 
var4 = module.function(a, b)

When i launch the main file in IDLE, I have the following error :

NameError: name 'function' is not defined

I have also tried : main.py :

from module import function
 var4 = function(a, b)

And got a module has no attribute 'function'

Does someone have an idea of what's wrong ? When I tried to import constants from the same module I had this kind of error as well

Dawindz
  • 11
  • 1
  • 3
  • 1
    Are you sure you're importing from the right `module.py`? What does `module.__file__` say? What about `dir(module)`. – jonrsharpe Mar 05 '17 at 15:08
  • For this particular problem, it would help to get more details about what the structure of your project is. You will have show a more realistic example of what you are experiencing. It is hard to determine exactly what is wrong with an arbitrary example like this. – idjaw Mar 05 '17 at 15:09
  • module.__file__ does not return the right file... is it possible python tries to import a module from standard modules that has the same name as mine ? there is the result for `dir(module)` : `['CommandCompiler', 'InteractiveConsole', 'InteractiveInterpreter', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'argparse', 'compile_command', 'interact', 'sys', 'traceback']` I assume it's the different files contained in the module, but therefore it's not the right one, i didn't code those ones ^^ – Dawindz Mar 05 '17 at 15:13
  • module name is code.py – Dawindz Mar 05 '17 at 15:13
  • Your module is code.py. What is in code.py. How are you trying to call the code inside code.py? Please provide the actual scenario you are dealing with. Also, what other files do you have in the project you are dealing with? – idjaw Mar 05 '17 at 15:17

1 Answers1

1

Most likely your module has the same name as one of Python's built-in modules. Try re-naming it and see if you still get the error.

EDIT: import code causes the interpreter to import Python's native module called code instead of the one you wrote, causing the NameError.

stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
  • it worked, thanks ! though I have the same issue, with an attribute error, for another module who's name does not belong to python standard lib... EDIT : error : `AttributeError: module 'decodeur' has no attribute 'decode'` – Dawindz Mar 05 '17 at 16:15
  • If you haven't figured it out, please post the exact line of code that is causing the error. I'm thinking `decode` is a function and you're calling it without parentheses (`decodeur.decode` instead of `decodeur.decode()`). – stelioslogothetis Mar 06 '17 at 16:10