473,785 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reload fails if module not in sys.path

So, it turns out that reload() fails if the module being reloaded isn't
in sys.path.

Maybe it could fall back to module.__file__ if the module isn't found
in sys.path??
.... or reload could just take an optional path parameter...

Or perhaps I'm the only one who thinks this is silly:
my_module = imp.load_module (module_name, *imp.find_modul e(module_name,p ath))
reload(my_modul e)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named whatever
I guess I could just deal with this by fiddling with sys.path or using
imp.load_module again, but.. um.. I like to complain. ;-)

The context here is that I'm loading user-defined modules as plugins,
and I don't want to keep the plugin directory in sys.path because of
potential module name conflicts.

Oct 20 '05 #1
4 4665
Lonnie Princehouse wrote:
So, it turns out that reload() fails if the module being reloaded isn't
in sys.path.

Maybe it could fall back to module.__file__ if the module isn't found
in sys.path??
... or reload could just take an optional path parameter...

Or perhaps I'm the only one who thinks this is silly:

my_module = imp.load_module (module_name, *imp.find_modul e(module_name,p ath))
reload(my_m odule)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named whatever
You appear to have failed to load some module called "whatever", which I
presume is the value of module_name?
I guess I could just deal with this by fiddling with sys.path or using
imp.load_module again, but.. um.. I like to complain. ;-)
That's OK, but you may find fiddling with sys.path is more productive :-)
The context here is that I'm loading user-defined modules as plugins,
and I don't want to keep the plugin directory in sys.path because of
potential module name conflicts.

Hmm. I know that if your module isn't in sys.modules your reload will fail:
import trPyCard # picking a moduule at random
import sys
del sys.modules['trPyCard']
reload(trPyCard ) Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: reload(): module trPyCard not in sys.modules


Apart from that, it would seem sensible not to try and use reload if you
haven't used a kosher import mechanism - there are many caveats on the
reload() documentation, and the mechanisms it uses aren't spelled out
anywhere but in the interpreter source.

It would seem easier just to ensure that the plugins directory(ies)
appear first on sys.path and use __import__(). imp is high magic.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Oct 21 '05 #2
Lonnie Princehouse wrote:
Maybe it could fall back to module.__file__ if the module isn't found
in sys.path??
... or reload could just take an optional path parameter...

Or perhaps I'm the only one who thinks this is silly:
my_module = imp.load_module (module_name, *imp.find_modul e(module_name,p ath))
reload(my_modul e)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named whatever


load_module doesn't do everything that import does.
I guess I could just deal with this by fiddling with sys.path or using
imp.load_module again, but.. um.. I like to complain. ;-)

The context here is that I'm loading user-defined modules as plugins,
and I don't want to keep the plugin directory in sys.path because of
potential module name conflicts.


so add the plugin-directory to the front of sys.path temporarily,
and remove it after you've imported the plugins. (this also allows
the plugin writers to split their plugins over multiple modules,
something that can often be quite nice)

</F>

Oct 21 '05 #3
It's not just load_module. Reload fails on modules imported normally
if their paths are no longer in sys.path.

Easy to reproduce example:

bash$ mkdir module_dir
bash$ touch module_dir/plugin.py
bash$ python
Python 2.4.1 (#1, Sep 25 2005, 15:12:45)
[GCC 3.4.3 20041125 (Gentoo 3.4.3-r1, ssp-3.4.3-0, pie-8.7.7)] on
linux2
Type "help", "copyright" , "credits" or "license" for more information.
import sys
sys.path.append ('module_dir')
import plugin
sys.path.pop() 'module_dir' reload(plugin) # plugin was imported correctly, but it can't be reloaded: Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named plugin
sys.modules['plugin'] # ... although it is sys.modules:

<module 'plugin' from 'module_dir/plugin.py'>

Oct 21 '05 #4
> That's OK, but you may find fiddling with sys.path is more productive :-)

Yeah, that's what I'm doing and it works just fine. When I stumbled
over this behavior yesterday it seemed (and still does) like a
low-priority bug in reload. I was hoping a guru would reply with
something like, "Of course that's how it is. If reload() tried to use
the __file__ attribute, a universe-ending paradox would ensue
because..."

Feh.

Oct 21 '05 #5

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

Similar topics

0
1663
by: Steve Holden | last post by:
For quite a while now I've been filling part of the navigation-bar on my home page with Python-related news extracted from O'Reilly's meerkat service. I've been experiencing intermittent problems for the past few days, and now suddenly the crontab-triggered script has stopped working completely, apparently due to a change in what Meerkat sends in response to my queries. I've not been able to find any description af changes to Meerkat,...
66
3911
by: Ellinghaus, Lance | last post by:
> > Other surprises: Deprecating reload() >Reload doesn't work the way most people think >it does: if you've got any references to the old module, >they stay around. They aren't replaced. >It was a good idea, but the implementation simply >doesn't do what the idea promises. I agree that it does not really work as most people think it does, but how
1
2146
by: Emmanuel | last post by:
Hi, I use a 'reload all' feature in my app, that allow to reload every module. I first try this version : import sys def Reload():
0
1328
by: Mustafa Thamer | last post by:
Hi, I'm using import hooks according to PEP 302, in order to load python files from a game PAK file. The game is C++ using embedded and extended Python (v2.33) and Boost. The importing works fine, but after modules are imported I can't reload them. I've tried 'reload(foo)' and 'PyImport_ReloadModule(pModPtr)', but both return 'ImportError: No module named foo'. Is it safe to assume that reload doesn't respect the import hook? That...
4
1802
by: test1dellboy3 | last post by:
Hi, I am a beginner using the python interpreter. To reduce typing effort, I created a module called "aliases.py" containing some aliases for objects I commonly use like - aliases.py : import filecmp, os, commands
0
3003
by: emu | last post by:
Hi All, I have an unmanaged C++ application that references a mixed mode image DLL (mixed managed and unmanaged). Under .NET 1.1 we could trust the dll (the mixed mode dll) by running the following command line: caspol.exe -polchgprompt off -machine -addgroup 1 -url "file://<UNC path to dll>\mixedMode.dll" FullTrust ame "GroupName" -polchgprompt on
3
2006
by: =?ISO-8859-1?Q?Gregory_Pi=F1ero?= | last post by:
Hi Python Experts, I hope I can explain this right. I'll try. Background: I have a module that I leave running in a server role. It has a module which has data in it that can change. So every nth time a function in the server gets called, I want to reload the module so it has the freshest data. But there's a lot of data so it takes 5-10 seconds to do a reload.
2
2759
by: dmitrey | last post by:
my Python module was changed in HDD (hardware disk drive), moreover, changed its location (but still present in sys.path). how can I reload a func "myfunc" from the module? (or howto reload whole module)? Thank you in advance, D.
0
1905
by: Rafe | last post by:
Hi, This seems to be an old question, and I've read back a bit, but rather than assume the answer is "you can't do that", I'd thought I'd post my version of the question along with a reproducible error to illustrate my confusion. My problem is that I'm using Python inside XSI (a 3D graphics application). If I want to restart Python, I have to restart XSI. This is no small amount of time wasted.
0
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9483
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
10346
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...
1
10096
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
9956
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
7504
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
6742
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();...
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2887
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.