Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6565577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:06:40+00:00 2026-05-25T14:06:40+00:00

In file1.py: def foo(): import file2 print I’m the old file1.py file2.bar() if __name__

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:06:40+00:00Added an answer on May 25, 2026 at 2:06 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take the following code example: File package1/__init__.py : from moduleB import foo print moduleB.__name__
My structure of file is foo/ __init__.py bar.py file __init__.py def abc(): print 'ABC'
Consider the following (broken) code: import functools class Foo(object): def __init__(self): def f(a,self,b): print
So I have two different files somewhat like this: file1.py from file2 import *
def start(fileName): fileReader = open(fileName) for row in fileReader: print row, if __name__ ==
Say I have the following code in code.py: from foo import bar from foo2
I tried to read a file in a view like this: def foo(request): f
Suppose I have following code: def foo(s): A dummy function foo. For example: >>>
I have the following code: import string def translate_non_alphanumerics(to_translate, translate_to='_'): not_letters_or_digits = u'!#%\'()*+,-./:;<=>?@[\]^_`{|}~' translate_table
I need to create instances of a custom object (Bar in the example below)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.