0

I am trying to hot swap a file in python. I am creating a game that takes a really long time to load. But I don't want to reload it every time. I am trying to change some code while the programme is in runtime. For example: I want to change this:

while True:
    print("Hello")

to this while in runtime:

while True:
    print("Hello World")

I looked hot swapping up for python and all of them are answers that I am not looking for. All the other answers change modules. I want to change the current file. Like java in eclipse. Please help!

D.White
  • 3
  • 3
  • 1
    It's unclear to me what you're trying to do. Do you want to change the code of a module which has already been loaded? The _current_ module? Do you want to textually modify the file on the disk? Do you want to dynamically rewrite the code while the module is being loaded? Please [edit your question](http://stackoverflow.com/posts/33316825/edit) and clarify. It'd be helpful if you explained what the existing answers you've seen said, and why they are not satisfactory to you. It'd also be helpful if you told us _why_ you want to do this. – Benjamin Hodgson Oct 24 '15 at 09:59

2 Answers2

0

I want to change the current file. Like java in eclipse.

When you modify Java code in Eclipse, the code is not currently running. In reality, Eclipse has a Java compiler built-in, which attempts to compile your code as you type it. That's how Eclipse is able to give you such fast feedback about syntax errors and type errors in your code. But it can't give you any information about the runtime behaviour of your code (whether or not it produces the right answer), because it doesn't run the code! You need to press the Run button for that.

So I think the question you really want to ask is not "Can I dynamically modify Python code?" (the answer to that is "Yes, but it's complicated and has caveats and is not a good idea") but "Does there exist a Python IDE which can give me feedback about syntax errors while I type?" The answer to that is an emphatic "Yes!". There's an extremely comprehensive list of options in this answer.

Community
  • 1
  • 1
Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157
  • Can you tell me how to modify currently-running code? – D.White Oct 24 '15 at 10:24
  • Why do you want to do that? It's not how Eclipse works. – Benjamin Hodgson Oct 24 '15 at 10:25
  • Do you want to write a Python program which dynamically modifies its own code? Or do you want to manually edit a file? You seem to be implying that it's the latter, in which case, I've already answered your question. I'm happy to help with anything about my answer which confuses you but you first have to tell me what you're confused about. – Benjamin Hodgson Oct 24 '15 at 10:29
  • Manually edit a file while it is in runtime and then get instant results – D.White Oct 24 '15 at 10:31
  • I've never heard of any programming language which allows that. It's not possible _in principle_: code which is executing has state; if you could change the code, the current state would be invalid. You have to stop the execution of the code to clear the state, and reload the file. – Benjamin Hodgson Oct 24 '15 at 10:35
0

You can use dynamic execution using exec function. Store the code you want to execute in a string and change it during runtime.

Amir
  • 820
  • 9
  • 30