0

I'm making a script in python that writes code to a .py file in the fourm of variables (e.g x = 6). The problem is when I import it to use the variables and then write some code to it I need to use reload() to refresh the import I quess and allow me to use the variable but it wont reload the .py file it will only reload the .pyc file.

Heres some code to help (not so sure it will :/)

I call a function that writes the data to the .py file and then calls a function to reloading the module but it ends up reload the .pyc

make(2, "name", "'Bob'")

def re(module):
        reload(module)
        print"Reloaded "+ str(module)


def make(0-1-2, var, data):
    if 0-1-2 == 1:
        TempF.write(var+" = "+data)
        TempF.write("\n")
        re(Temp)

    if 0-1-2 == 2:
        PermF.write(var+" = "+data)
        PermF.write("\n")
        re(Perm)

    if 0-1-2 == 0:
        MiscF.write(var+" = "+data)
        MiscF.write("\n")
        re(Misc)


    Reloaded <module 'Perm' from 'C:\Users\Admin\PycharmProjects\DeanRobbieRowe\Perm.pyc'>
Zrman2012
  • 19
  • 4
  • Why are you editing a module that is presumably loaded at the time of use? That is typically for interpreter usage. – Rob Foley Sep 02 '15 at 20:33
  • 6
    I very strongly recommend rethinking your algorithm instead of pursuing this method of repeatedly modifying and reimporting a source file. – TigerhawkT3 Sep 02 '15 at 20:33
  • Well bassicaly I need to be able to write and save variables and be able to use them directly after they get written. – Zrman2012 Sep 02 '15 at 20:35
  • 1
    And the way you do that is by writing an ordinary text file that is _separate_ from your source code. Take a look at file I/O in the [official Python tutorial](https://docs.python.org/3.4/tutorial). – TigerhawkT3 Sep 02 '15 at 20:36
  • Yes but how exactly do I load it into my script? – Zrman2012 Sep 02 '15 at 20:41
  • 1
    By reading the file. It's kind of hard to tell what you actually wanted to do in the first place, hidden as it is by this [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – TigerhawkT3 Sep 02 '15 at 20:42
  • 1
    As indicated, use the file I/O operations to write and read files. I would use [`json`](https://docs.python.org/3/library/json.html) as the file format. – dsh Sep 02 '15 at 20:51
  • If this information will be used while the program is running, save it to variables. If you want to be able to recall that information after closing and restarting the program, save it to a file on exit and then read it back in on startup. – TigerhawkT3 Sep 02 '15 at 20:52

1 Answers1

0

Try using the file object's .flush() method after writing.

So:

make(2, "name", "'Bob'")

def re(module):
        reload(module)
        print"Reloaded "+ str(module)


def make(0-1-2, var, data):
    if 0-1-2 == 1:
        TempF.write(var+" = "+data)
        TempF.write("\n")
        TempF.flush()
        re(Temp)

    if 0-1-2 == 2:
        PermF.write(var+" = "+data)
        PermF.write("\n")
        PermF.flush()
        re(Perm)

    if 0-1-2 == 0:
        MiscF.write(var+" = "+data)
        MiscF.write("\n")
        MiscF.flush()
        re(Misc)

See: what exactly the python's file.flush() is doing?

Community
  • 1
  • 1
laphiloctete
  • 476
  • 1
  • 6
  • 20