简体   繁体   English

如何在不出现 NameError 的情况下将不同目录中的函数导入到我的主代码中?

[英]How do I Import a function from a different directory to my main code without getting a NameError?

I'm using Python 3.8.5.我正在使用 Python 3.8.5。 I was aiming at creating a simple addition function (and yes, I'm aware Python has an in-built one).我的目标是创建一个简单的加法函数(是的,我知道 Python 有一个内置函数)。 I created the function satisfactorily, but wasn't able to import it to the program of my choice.我令人满意地创建了该函数,但无法将其导入到我选择的程序中。 I keep getting NameErrors, saying that the name of my function has not been defined.我不断收到 NameErrors,说我的函数名称尚未定义。 Here's the code to the function:这是函数的代码:

def add_func(x, y):
    sum = x + y
    sum_str = str(sum)
    print("The sum is " + sum_str)

And here's where the issues begin:这就是问题开始的地方:

import sys
import os
sys.path.append('C:\\Users\\User_name\\Desktop\\Folder\\PYTHON\\Functions')
import addition
x = (input("Enter no. 1: "))
y = (input("Enter no. 2: "))
add_func(x,y)

I have an init file in the folder named Functions.我在名为 Functions 的文件夹中有一个init文件。 I've scoured the web for hours searching for a solution to my problem, but have been unable to find one that: a) I can understand (still learning Python) b) Has worked我已经在网上搜索了几个小时来寻找解决我的问题的方法,但一直找不到:a) 我能理解(仍在学习 Python)b) 已经奏效了

I'd really appreciate it if somebody could guide me on how to fix this issue, and explain to me what I've done wrong (dumb it down as much as possible)如果有人能指导我如何解决这个问题,并向我解释我做错了什么(尽可能把它写下来),我将不胜感激

If addition is the name of your .py file, and it has been successfully imported, then to use your add_func function you would need to write:如果 add 是您的 .py 文件的名称,并且已成功导入,则要使用 add_func 函数,您需要编写:

addition.add_func(x, y)

If you wanted to be able to use it without the addition., you can change the import statement to:如果您希望能够在不添加.的情况下使用它,您可以将导入语句更改为:

from addition import add_func

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从与当前目录相同“级别”的目录中的文件中导入 function - How do I import a function from a file in a directory on the same 'level' as my current directory 以超级用户身份运行主代码时,如何从其他用户目录导入函数? - can I import a function from other user directory while running the main code as super user? 如何从目录导入其他python代码? - How do I import other python code from a directory? 如何在我的主代码旁边运行一个函数? - How do I run a function alongside my main code? 如何使函数“ addlevels”在我的主代码中起作用? - How Do I make the function “addlevels” work in my main code? 如何从父目录中的文件导入 function - How do i import a function from a file in a parent directory 如何从python中的不同目录导入函数 - How to Import Function from different directory in python 如何从不同目录中的 function 导入变量 - How to import a variable from a function in a different directory 我是否需要将项目目录添加到每个脚本中的系统路径以从另一个目录导入函数? - Do I need to add my project directory to the system path in every script to import a function from another directory? 如何从 main.py (在根目录中)将 object 导入到子目录中包含的模块中? - How do I import an object from main.py (in the root directory), into a module contained in a subdirectory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM