Python 如何从另一个文件调用变量?

Python 如何从另一个文件调用变量?,python,Python,我有两个文件。第一个文件,我们称之为“Main.py”。第二个是“file1.py” 我想从Main.py调用一个变量,并将该值写入名为“tempFile.txt”的新文件。我已尝试导入“Main.py”,但出现属性错误 下面是“File1.py”的一个示例 我试过其他方法寻找答案,但对于这个具体问题没有明确的答案 编辑: Main.py import os import sys import file1 X_Value = 1000 Y_Value = 1000 # F

我有两个文件。第一个文件,我们称之为“Main.py”。第二个是“file1.py”

我想从Main.py调用一个变量,并将该值写入名为“tempFile.txt”的新文件。我已尝试导入“Main.py”,但出现属性错误

下面是“File1.py”的一个示例

我试过其他方法寻找答案,但对于这个具体问题没有明确的答案

编辑:

Main.py

 import os
 import sys
 import file1



 X_Value = 1000
 Y_Value = 1000



 # For statement that manipulates the values, too long to post.
 for "something":
     if "something":


 # after the values are calculated, kick it to the console
 print "X Value: " + str(X_Value) + "\n"
 print "Y Value: " + str(Y_Value) + "\n"
我需要在处理Main.py之后将变量的值写入tempFile

编辑:

我已尝试在Main.py中创建tempFile,但由于某些原因,我用于读取tempFile并将值添加到列表中的函数没有出现,但是,在我删除Main.py中创建的tempFile并取消对File1.py中的write函数的注释后,这些值确实会出现。请尝试导入它

from YourFileName import *
而且打电话的时候,

YourFileName.tempFile 
如果只想调用变量

from YourFileName import VarName1

您呈现的代码创建循环导入;i、 e.main.py导入file1.py,file1.py导入main.py。那不行。我建议将write_values()更改为接受两个参数,然后将它们从main.py传入,并取消将main导入file1:

main.py:

import os
import sys
import file1

X_Value = 1000
Y_Value = 1000

file1.writeValues(X_Value, Y_Value)
file1.py:

import os
import sys

# This function should write the values from Main.py to a tempFile 
# and reads the contents to store into a list.
def writeValues(X_Value, Y_Value):
    tempFile = open('tempFile.txt', 'w+')        
    tempFile.write(str(X_Value))
    tempFile.write("\n")
    tempFile.write(str(Y_Value))
    tempFile.close()
    zoneValues = [line.rstrip('\n') for line in open('tempBeds.txt')]
    print zoneValues

def readZoneValues(): # Creates a list from values in tempFile.txt
    valuesList = [line.rstrip('\n') for line in open('tempFile.txt')]
    print valuesList

阅读了解pythonI中的包和模块是如何工作的。如果代码中没有看到导入Main,您是否真的导入了它?如果是这样的话,
AttributeError
是否出现在
import Main
语句中,或者当您试图使用
Main.py
中的内容时?如果它在
import Main
语句中,那么错误就在该文件中。否则,您需要以
Main.X_值
Main.Y_值
@Billy我在中编辑了“导入主文件”。下面是我收到的错误:AttributeError:“module”对象没有属性“writeValues”@Dan调用变量时请尝试main.tempfiles因为您在
writeValues
上获得了
AttributeError
,似乎其他模块正在尝试使用此文件中的
writeValues
函数。对吗?您是否将
file1.py
main.py
作为主模块运行?我仍然会遇到以下错误:AttributeError:“module”对象没有属性“writeValue”
*
。它将导入可能覆盖现有变量名称空间的变量。我建议不要使用它。在这种情况下,这是创建循环导入,这就是为什么会触发AttributeError。就像Francisco_Couzo所说的那样?我得到了以下错误:TypeError:writeValues()正好接受2个参数(给定0)。为什么?@Dan:是的,就像Francisco说的,但我已经对AttributeError和示例代码进行了解释(你发表评论时我正在处理这个问题,但首先提供了解释以帮助你开始)。TypeError是因为调用WriteValue时未提供参数。
import os
import sys

# This function should write the values from Main.py to a tempFile 
# and reads the contents to store into a list.
def writeValues(X_Value, Y_Value):
    tempFile = open('tempFile.txt', 'w+')        
    tempFile.write(str(X_Value))
    tempFile.write("\n")
    tempFile.write(str(Y_Value))
    tempFile.close()
    zoneValues = [line.rstrip('\n') for line in open('tempBeds.txt')]
    print zoneValues

def readZoneValues(): # Creates a list from values in tempFile.txt
    valuesList = [line.rstrip('\n') for line in open('tempFile.txt')]
    print valuesList