从单独的python文件导入函数

从单独的python文件导入函数,python,python-3.x,namespaces,Python,Python 3.x,Namespaces,我正在尝试将函数从一个文件导入到另一个文件 比如说 在我的文件“main.py”中,我有以下代码: from helper_funcs import * a = 10 print(square()) def square(): return a*a 在我的文件“helper_funcs.py”中,我有以下代码: from helper_funcs import * a = 10 print(square()) def square(): return a*a 显然,这不起作用,因

我正在尝试将函数从一个文件导入到另一个文件

比如说 在我的文件“main.py”中,我有以下代码:

from helper_funcs import *
a = 10
print(square())
def square():
  return a*a
在我的文件“helper_funcs.py”中,我有以下代码:

from helper_funcs import *
a = 10
print(square())
def square():
  return a*a
显然,这不起作用,因为我的“helper_funcs.py”文件中没有定义“a”。它可能与名称空间有关。是否有一种方法可以将此函数与主文件中的变量一起使用


我不想将变量“a”作为一个参数传递。

试试这个

您的函数另存为square.py

def square(a):
  return a*a
在主代码中,按如下方式导入函数,但不要使用
from square.py import square

from square import square
a = 10
print(square(a))

但在这里,您将变量a

pass
a
作为参数传递给
square
,并将
square
的定义更改为
square(a)
。不过你应该这么做。这听起来像是一个XY问题。“我不想把变量‘a’作为一个Aragement传递。”-为什么不呢?这是正确的方法。从
main.py
a
导入
helper\u funcs.py
中,然后将其作为
kwargs
或其他内容传递。