0

Consider the following Python (2.7.9) code:

test.py

import test2
import time

while True:
    print test2.getData()
    time.sleep(1)

test2.py

def getData ():
    return [1,2,3]

Running with:

python -u test.py

If I modify test2.py while test.py is running (say, changing it to return [4,5,6]), the output of test.py does not change. This is not unexpected.

However, I'd like changes to test2.py to be reflected in the output. Is there a way to do this? E.g. something like reparsing test2.py every time test2.getData() is invoked?

Other things tried, from comments:

  • Moving import test2 into the loop.
  • Removing test2.pyc while test is running (with import in and out of loop).
Jason C
  • 38,729
  • 14
  • 126
  • 182
  • I'm assuming your code is converted into byte code before its ran, so modifications to any file are not reflected during execution – OneCricketeer Dec 04 '16 at 15:36
  • Maybe you could try to move the import statement into the loop, but that still seems strange why you want this – OneCricketeer Dec 04 '16 at 15:37
  • @cricket_007 test2.pyc is generated, although removing it after modifying test2.py has no effect on the output. – Jason C Dec 04 '16 at 15:38
  • @cricket_007 The reason I want this is in my real code test2.py is generated by another program, and occasionally regenerated with different data. I could store the data in a text file or database or something and read it every time through the loop, but I'm curious if I can just leave it as Python source because it simplifies my "real" equivalent of test.py a bit. – Jason C Dec 04 '16 at 15:39
  • Right because test1 only imported test2 once. Before any modification – OneCricketeer Dec 04 '16 at 15:40
  • @cricket_007 Moving the import into the loop has no effect. Just tested it. It seemed like a good idea, though, ha. – Jason C Dec 04 '16 at 15:41
  • 1
    Hmm. Worth a shot. Anyways - yeah, reading the file might be the only option. Or cache in memory somehow, like Redis or Sqlite memory table – OneCricketeer Dec 04 '16 at 15:44
  • In the loop you could check to see if the date of the `test2.py` script file is newer than the `test2.pyc` compiled-byte-code file. If it is, you could then re-import the `test2` module which should cause a new `test2.pyc` file it to be automatically re-compiled and used. – martineau Dec 04 '16 at 17:09
  • 1
    @cricket_007 I finally figured this out; just needed to improve my search keywords. It's [`reload(test2)`](http://stackoverflow.com/a/684186/616460). Pretty straightforward. Although too late, since I ended up just storing the data in a CSV file and parsing it (which is pretty easy, too). – Jason C Dec 09 '16 at 00:57

1 Answers1

1

If I recall properly, python code will be converted in to a byte code before being executed so there are no ways of changing the code when running it. What I suggest you do is to create a global variable or a class instead and change that instead. Alternate solution will be to write the data to a file but doesn't benefit much since it needs access to the file system and will not be as fast as variables.

Taichi Kato
  • 602
  • 9
  • 24