473,737 Members | 5,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.jo in(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.key s()[:]
d_b(d)
for k in sys.modules.key s():
if k not in OK: del sys.modules[k]

#this doesn't
for d in 'a','c':
OM = sys.modules.cop y()
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 1964
Robin Becker <ro***@reportla b.com> wrote in message news:<40******* *******@chamoni x.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***@reportla b.com> wrote in message news:<40******* *******@chamoni x.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
1959
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 inside a package, the invocation will fail with a "Module not found" error. This PEP is aimed at fixing that :) Cheers, Nick.
15
2609
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 for Python 2.5. Previously, posting of a draft version of the PEP to python-dev and python-list didn't actually generate any responses. I'm not sure if that's an indication that people don't see the restriction to top-level modules as a problem...
7
2073
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 old lines at the end: if __name__ == "__main__": unittest.main() This is great, because each of the modules are runnable, and I can select classes or tests to run on the commandline if I want to. However, running all the tests from e.g. a...
2
3353
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 does System.out.println("$^%$%^^" ); //assume those are Japanese characters that are multibyte under EUC-JP The resulting output looks NOTHING like the characters I typed in. Apparently the character set being used to read the literal is...
4
1756
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 creating two files and importing them.
2
2037
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 about a circular dependency in Modules/signalmodule.o. I'm new to makefiles and makefile rules, so I can understand what this means, but I don't know how I can fix this problem. Looks like I'll need to rewrite one or more makefile rules and...
7
4304
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, plus of course the modules associated with the forms themselves. My practice has been to create a seperate module to handle specific groupings of code, for example, a seperate module to handle clipboard actions, window sizing actions, etc.
13
2362
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 (replacement for PHP's include() statement). My problem is, in PHP if you open a database connection it's always in scope for the duration of the script. Even if you use an abstraction layer ($db = DB::connect(...)) you can `global $db` and bring...
3
1827
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 imports ) Take Example :- in A.py :- import B print dir() # no problems we can see B which contain re module and C module
0
8786
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9470
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9258
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9207
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6749
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6052
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.