简体   繁体   中英

Python “module 'fileName' has no attribute 'functionName'”

I have a main.py file accessing a number of other modules.

I want to call the function using fileName.fileFunction()

In the module I want to access, I am importing the main.py file and the code is as follows:

import main
def fileFunction():
    statements
    statements
    var = input("enter a name")
    statements 
    return var

I want to access the value the user enters into var and then use that in my main module but I get the following error:

module 'fileName' has no attribute 'fileFunction'

In your main file you called fileName.fileFunction() and in your fileName you imported main.

While executing the fileName module your main file will execute first in which you have fileFunction() which is not defined.

import the fileName module in your main and remove import main line from the fileName module.

Main.py

import fileName
print(fileName.fileFunction())

fileName.py

def fileFunction():
  a=input("Enter a name : ")
  return a

I think you've got it backwards.

fileName.py

#!/usr/bin/python

def fileFunction():
    var = print("enter a name")
    return var

main.py

#!/usr/bin/python

import fileName

fileName.fileFunction()

command :

$ python main.py
enter a name

you try to do like this ?

file_name.py

def file_function():
    retunrn 'file_name'

main.py

from file_name import file_function

your_file.py

import main
print(main.file_function())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM