Python 有没有办法将文件中一个函数中的变量访问到另一个文件

Python 有没有办法将文件中一个函数中的变量访问到另一个文件,python,function,import,call,python-3.5,Python,Function,Import,Call,Python 3.5,我有两个文件prgm.py和test.py 1.prgm.py def move(self) H=newtest.myfunction() i= H.index(z) user=newuser.my_function() print(user[i]) 如何在另一个名为test.py的代码中获取用户[i]在另一个文件中使用import语句 这样-从prgm导入移动 注意:要使其正常工作,两个文件必须位于同一文件夹中,或者导入文件的路径必须位于PYTHONPATH

我有两个文件prgm.py和test.py

1.prgm.py

def move(self)
    H=newtest.myfunction()
    i= H.index(z)
    user=newuser.my_function() 
    print(user[i])

如何在另一个名为test.py的代码中获取用户[i]在另一个文件中使用import语句

这样-
从prgm导入移动


注意:要使其正常工作,两个文件必须位于同一文件夹中,或者导入文件的路径必须位于PYTHONPATH中,而不是打印结果,您只需返回它即可。在第二个文件中,您只需从这个源文件导入函数并调用它

在这种情况下,
move
实际上是一个类方法,因此需要导入整个类并在第二个文件中对其进行实例验证

prgm.py

class Example:
   def move(self):
       H = newtest.myfunction() 
       i = H.index(z)
       user = newuser.my_function()
       return user[i]
from prgm import Example

example = Example()
user = example.move()
# do things with user
test.py

class Example:
   def move(self):
       H = newtest.myfunction() 
       i = H.index(z)
       user = newuser.my_function()
       return user[i]
from prgm import Example

example = Example()
user = example.move()
# do things with user

您的代码不清楚,但基本上应该导入prgm并调用prgm.your_val或prgm.your_fun()