1

I have two files. The first file, we'll call it "Main.py". The second, "file1.py".

I want to be call a variable from Main.py and write the value to a new file called "tempFile.txt". I've tried importing "Main.py" but I get an Attritbute error.

Here's an example of "File1.py"

import os
import sys
import main

 # This function should write the values from Main.py to a tempFile 

 # and reads the contents to store into a list.

def writeValues():

    tempFile = open('tempFile.txt', 'w+')        
    tempFile.write(str(X_Value))
    tempFile.write("\n")
    tempFile.write(str(Y_Value))
    zoneValues = [line.rstrip('\n') for line in open('tempFile.txt')]
    print zoneValues

 # X_Value and Y_Value are the variables in Main.py I am trying to access



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

I've tried other looking for answers but there was no clear answer to this specific issue.

EDIT:

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"

I need the values of the variables to be written to the tempFile after Main.py has been processed.

EDIT:

I have tried having the tempFile created in Main.py, but for some reason my function for reading the tempFile and adding the values to a list do not appear, however, the values DO APPEAR after I delete the tempFile creation in Main.py and uncomment the write function in File1.py

EL_DANIMAL
  • 92
  • 1
  • 12
  • 3
    Read [this answer](http://stackoverflow.com/questions/7948494/whats-the-difference-between-a-python-module-and-a-python-package) to find out how packages and modules work in python –  Nov 10 '16 at 20:02
  • 1
    I don't see `import Main` in your code, are you actually importing it? If so, does the `AttributeError` occur on the `import Main` statement or when you're trying to use something from `Main.py`? If it's on the `import Main` statement then the error is in that file. Otherwise you need to access the values as `Main.X_Value` and `Main.Y_Value` – Billy Nov 10 '16 at 20:04
  • @Billy I edited the "import main" in. Here is the error I recieve: AttributeError: 'module' object has no attribute 'writeValues' – EL_DANIMAL Nov 10 '16 at 20:06
  • @Dan when calling the variables try main.tempFile – Simeon Aleksov Nov 10 '16 at 20:08
  • Since you're getting an `AttributeError` on `writeValues`, it seems as if some other module is trying to use the `writeValues` function in this file. is that correct? Are you running `file1.py` or `main.py` as your main module? – Billy Nov 10 '16 at 20:13
  • @Billy running main.py – EL_DANIMAL Nov 10 '16 at 20:14
  • @Billy I've tried having main.py create and write the values to the temp File, but I also have another function in File1.py that reads the contents of the tempFile and store the values in a list. – EL_DANIMAL Nov 10 '16 at 20:15
  • Do you have `import File1` in your main file? Then `File1.writeValues()`? Hard to answer without the source in `main.py` – Billy Nov 10 '16 at 20:16
  • @Billy updated the OP – EL_DANIMAL Nov 10 '16 at 20:27
  • @Billy I need the values of the variables to be written to the tempFile after Main.py has been processed – EL_DANIMAL Nov 10 '16 at 20:30

2 Answers2

1

The code you're presenting creates a circular import; i.e. main.py imports file1.py and file1.py imports main.py. That doesn't work. I would recommend changing write_values() to accept two parameters, and then passing them in from main.py, and eliminating the import of main into 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
GreenMatt
  • 18,244
  • 7
  • 53
  • 79
  • Like Francisco_Couzo said? – EL_DANIMAL Nov 10 '16 at 20:49
  • I get this error: TypeError: writeValues() takes exactly 2 arguments (0 given). Why? – EL_DANIMAL Nov 10 '16 at 20:51
  • @Dan: Yes, like Francisco said, but I've provided an explanation for the AttributeError and example code (I was working on that when you commented, but had provided the explanation 1st to help you get started). The TypeError was because you didn't provide arguments when you called writeValues. – GreenMatt Nov 10 '16 at 20:58
0

Try importing it.

from YourFileName import *

Also, when calling,

YourFileName.tempFile 

If you want to call only your variable then,

from YourFileName import VarName1
Simeon Aleksov
  • 1,275
  • 1
  • 13
  • 25
  • I still get this error : AttributeError: 'module' object has no attribute 'writeValues' – EL_DANIMAL Nov 10 '16 at 20:07
  • 3
    `*` is NEVER really a good choice. It would import variables that could overwrite namespace of existing variables. I would advice against using it. –  Nov 10 '16 at 20:12
  • In this case, that's creating a circular import, which is why an AttributeError is triggered. – GreenMatt Nov 10 '16 at 21:01