473,395 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

sys.modules strangeness

We had some legacy applications that used import to get parts of documents in.
When run separately these worked fine, but failed when run as a single process
because they both imported ch1 (after jumping to their home dirs and placing
these on the path). Clearly the first to run used up ch1.

I have a simple test script below. It seems I cannot just restore the original
sys.modules and leave the modules to die, but actually need to del the relevant
entries. Are modules somehow cached somewhere? What's the right way to unload a
module (assuming I can remove non sys refs).

#timport.py start#######################
import sys, os
def d_b(d):
os.chdir(d)
cwd = os.getcwd()
sys.path.insert(0,os.getcwd())
import b
print 'Expecting %s:'%os.path.join(d,'b.py'),
b.run()
sys.path.remove(cwd)
os.chdir('..')

for d in 'a','c':
fn = os.path.join(d,'b.py')
f = open(fn,'r')
print 'The file %s is\n#######\n%s#######\n' % (fn,f.read())
f.close()

#this works
for d in 'a','c':
OK = sys.modules.keys()[:]
d_b(d)
for k in sys.modules.keys():
if k not in OK: del sys.modules[k]

#this doesn't
for d in 'a','c':
OM = sys.modules.copy()
d_b(d)
sys.modules = OM
#a/b.py##############################
def run():
print 'my file is', __file__, 'I am a\\b.py'
#c/b.py##############################
def run():
print 'my file is', __file__, 'I am c\\b.py'
#outputput###########################
The file a\b.py is
#######
def run():
print 'my file is', __file__, 'I am a\\b.py'
#######

The file c\b.py is
#######
def run():
print 'my file is', __file__, 'I am c\\b.py'
#######

Expecting a\b.py: my file is C:\Tmp\IIII\a\b.pyc I am a\b.py
Expecting c\b.py: my file is C:\Tmp\IIII\c\b.pyc I am c\b.py
Expecting a\b.py: my file is C:\Tmp\IIII\a\b.pyc I am a\b.py
Expecting c\b.py: my file is C:\Tmp\IIII\a\b.pyc I am a\b.py
--
Robin Becker
Jul 18 '05 #1
2 1934
Robin Becker <ro***@reportlab.com> wrote in message news:<40**************@chamonix.reportlab.co.uk>.. .
We had some legacy applications that used import to get parts of documents in.
When run separately these worked fine, but failed when run as a single process
because they both imported ch1 (after jumping to their home dirs and placing
these on the path). Clearly the first to run used up ch1.


Have you tried reload(ch1)? (see section 2.1, "Built-in Functions",
in the Python Library Reference.
Jul 18 '05 #2
A. Lloyd Flanagan wrote:
Robin Becker <ro***@reportlab.com> wrote in message news:<40**************@chamonix.reportlab.co.uk>.. .
We had some legacy applications that used import to get parts of documents in.
When run separately these worked fine, but failed when run as a single process
because they both imported ch1 (after jumping to their home dirs and placing
these on the path). Clearly the first to run used up ch1.

Have you tried reload(ch1)? (see section 2.1, "Built-in Functions",
in the Python Library Reference.


I know that reload works. I was trying to restore the modules state to a
specific point as in general I didn't know where or which modules the apps could
import.

The original problem has gone away as I decided to exec the code files in a
specific namespace rather than import them. I am still curious why replacing the
current version of sys.modules with an earlier copy doesn't reset the modules list.
--
Robin Becker
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nick Coghlan | last post by:
Anyone playing with the CPython interpreter's new command line switch might have noticed that it only works with top-level modules (i.e. scripts that are directly on sys.path). If the script is...
15
by: Nick Coghlan | last post by:
Python 2.4's -m command line switch only works for modules directly on sys.path. Trying to use it with modules inside packages will fail with a "Module not found" error. This PEP aims to fix that...
7
by: Jorgen Grahn | last post by:
I have a set of tests in different modules: test_foo.py, test_bar.py and so on. All of these use the simplest possible internal layout: a number of classes containing test*() methods, and the good...
2
by: Robert M. Gary | last post by:
I'm using JRE 1.5 on Solaris Japanese (Sparc). The JVM claims its default character set is EUC-JP I'm seeing two strange things when using Japanese character sets... 1) If I write a program that...
4
by: Misto . | last post by:
Hi folks! Short: There is a way to dumplicate a module ? I tried copy.deepcopy(module) but hangs with an error (also with standard modules ).. The only solution that I have by now is...
2
by: James Buchanan | last post by:
Hi group, I'm preparing Python 2.4.2 for the upcoming Minix 3.x release, and I have problems with make. configure runs fine and creates the makefile, but right at the end ends with an error...
7
by: Lauren Quantrell | last post by:
At running the risk of asking how big is too big... Is there a rule of thumb or a best practice that says I may have too many modules? I currently have a Access2K app with about 30 code modules,...
13
by: Robin Haswell | last post by:
Hey people I'm an experience PHP programmer who's been writing python for a couple of weeks now. I'm writing quite a large application which I've decided to break down in to lots of modules...
3
by: Mohamed Yousef | last post by:
Hello , The problem I'm asking about is how can imported modules be aware of other imported modules so they don't have to re-import them (avoiding importing problems and Consicing code and...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.