Old code still being executed in ipython after files have been modified
Asked Answered
C

2

6

In file1.py:

def foo():
    import file2
    print "I'm the old file1.py"
    file2.bar()

if __name__ == '__main__':
    foo()

In file2.py

print "I'm the old file2.py"

def bar():
    print "I'm in the old file2.bar()"

On line 5 of the interactive session below, after making modifications to file1.py and file2.py changing all three occurrences of the word old to new, the new code in file2.py is still not used.

wim@wim-ubuntu:~/sandpit$ ipython
>>> run file1.py
I'm the old file2.py
I'm the old file1.py
I'm in the old file2.bar()
>>> !rm file2.pyc 
>>> # modify file1, file2
>>> run file1.py
I'm the new file1.py
I'm in the old file2.bar()

Where is it getting the old code from file2.py from?

I must misunderstand something, because I thought (from ipython help on run):

The file is executed in a namespace initially consisting only of __name__ == '__main__' and sys.argv constructed as indicated. It thus sees its environment as if it were being run as a stand-alone program

I've deleted the .pyc file, and can see from the command whos that there is no file2 module present in the namespace. But why is the import not executed again when running file1 a second time?

Christmann answered 14/9, 2011 at 15:2 Comment(3)
The "deep reload" in IPython.lib.dreload (or IPython.deep_reload in 0.10.x) might help you with this.Nealson
In case someone has something similar: I though I had a similar problem, except I ran the code with regular python (not in interpreter mode). Even deleting .pyc files was of no help. As it turned out, it was just Tmux showing me old output when I scrolled up (using the Vi plugin).Wagon
do you just basically run a new ipython session each time?Anastomosis
A
2

run does not start a new Python process, but rather executes your code in the current one--not in the current namespace, but in the current Python process, the note from the documentation explains. Because of this, sys.modules is still around and the old cached module is used. (Are you familiar with the way Python caches imported modules normally?)

To fix this, run in a new Python process each time. reload is more than a little problematic and can lead to headaches that I find aren't worth it.

Airflow answered 14/9, 2011 at 15:16 Comment(4)
what does "To fix this, run in a new Python process each time. reload is more than a little problematic and can lead to headaches that I find aren't worth it." mean? does is it mean I need to start a new ipython session each time I update my scripts?Anastomosis
@CharlieParker The relevant issue comes up when you are running a script that does import foo, and you have edited foo.py. In that case, you will not pick up the edits. In general, it's probably easier to think about things when you just run any scripts fresh each time.Airflow
I want my scripts to run fresh each time without me having to close ipython, do you know how one does that?Anastomosis
@CharlieParker I don't think it's impossible if you want them to share a Python environment, rather than run the script in a separate process. To run it in a separate process, use ! for a system call or use the subprocess module.Airflow
B
-1

I just met this problem and the solution is simple

This is a text-displaying problem,some code cannot show are hidden from us,what you should do is to copy everything to a .txt file and you will see lots of old stuff,delete them and copy the code back to your file.py

old.py

print("Im the new one")

But in a new text file,you will see:

newText.txt


print("I am the old one!!!!")

#This is the old codeeeeeeeeeeeeeeeeee

print("Old codesssssssssss")


I know the explaination is weird,but that's the truth that I saw!

Bartley answered 8/8, 2022 at 13:45 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Leatheroid

© 2022 - 2024 — McMap. All rights reserved.