Python从其他文件导入变量

Python从其他文件导入变量,python,function,file,variables,import,Python,Function,File,Variables,Import,我在同一目录中有3个文件:test1.py、test2.py和init.py 在test1.py中,我有以下代码: def test_function(): a = "aaa" from test1 import * def test_function2(): print(a) test_function2() 在test2.py中,我有以下代码: def test_function(): a = "aaa" from test1 import * de

我在同一目录中有3个文件:test1.py、test2.py和init.py

在test1.py中,我有以下代码:

def test_function():
    a = "aaa"
from test1 import *


def test_function2():
    print(a)


test_function2()
在test2.py中,我有以下代码:

def test_function():
    a = "aaa"
from test1 import *


def test_function2():
    print(a)


test_function2()
我可以将“test_function”(并调用该函数)导入test2.py,但不能在test2.py中使用变量“a”

错误:未解析的引用“a”

我想知道是否可以在test2.py中使用“a”。

在Python中,仅在函数内部引用的变量是隐式全局变量如果在函数体中的任何位置为变量赋值,则假定该变量为局部变量,除非明确声明为全局变量。

因此,将变量
a
设为全局变量,并在
test1
模块中调用
test\u函数()
,以便在加载模块时将
a
设为全局变量

test1.py

def test_function():
  global a
  a = "aaa"

test_function() 
from test1 import *

def test_function2():
  print(a)


test_function2()
a = ""
def test_function():
    global a
    a = "aaa"
import test1

def test_function2():
    print(test1.a)

test1.test_function()
test_function2()
test2.py

def test_function():
  global a
  a = "aaa"

test_function() 
from test1 import *

def test_function2():
  print(a)


test_function2()
a = ""
def test_function():
    global a
    a = "aaa"
import test1

def test_function2():
    print(test1.a)

test1.test_function()
test_function2()

在test1.py中,可以有一个函数返回变量a的值

def get_a():
归还
当您在test2.py中时,可以调用
get\u a()

因此,在test2.py中,这样做实际上是为了从test1.py移过a的值

from test1 import *

a = get_a()

def test_function2():
    print(a)


test_function2()
Test1.py

def test_function():
    a = "aaa"
    return a
Test2.py

import test1


def test_function2():
    print(test1.test_function())


test_function2()

py的代码如下所示

def H():
    global a
    a = "aaa"
H()
import test1 as o
global a
o.H()
print(o.a)
py的代码如下所示

def H():
    global a
    a = "aaa"
H()
import test1 as o
global a
o.H()
print(o.a)
这将允许您调用testone H

您的代码工作正常(在test1_函数之外定义了“a”),能够打印“a”。因此,请尝试以下方法: 1.确保它是test1中的全局变量。 2.在交互式会话中导入test1并找出bug。 3.仔细检查环境设置

谢谢!
:)

a
仅在
测试函数()的范围内定义。您必须在函数外部定义它,并使用
global
关键字访问它。这就是它看起来的样子:

test1.py

def test_function():
  global a
  a = "aaa"

test_function() 
from test1 import *

def test_function2():
  print(a)


test_function2()
a = ""
def test_function():
    global a
    a = "aaa"
import test1

def test_function2():
    print(test1.a)

test1.test_function()
test_function2()
test2.py

def test_function():
  global a
  a = "aaa"

test_function() 
from test1 import *

def test_function2():
  print(a)


test_function2()
a = ""
def test_function():
    global a
    a = "aaa"
import test1

def test_function2():
    print(test1.a)

test1.test_function()
test_function2()

我在“test_function”中尝试将变量“a”设置为全局变量,但不起作用,我在“test_function2”中尝试将其设置为全局变量,但它表示未在模块级别定义的变量。test1.py无效(您希望第二行缩进)?很抱歉,粘贴代码时出现了错误,这没有帮助。我需要在其中一个函数中找到一个解决方案。我没有很好地解释自己,test_函数应该只有变量“a”,什么都不做(在本例中不返回a)@redhunter,如果你不想要返回函数,你可以创建一个类,在其中添加“a”,然后在test2中引用它,但这就像“烧房子杀蟑螂”