473,616 Members | 2,970 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

import, from and reload

I understand that after you import something once, you can reload it to
pick up new changes. But does reload work with from statements? I tried
this:

from X import *

and then did my testing. I changed X and tried to reload it, but that
didn't seem to work. I figure the reason is because the module itself
doesn't exist as an object, only its names do. But I couldn't figure out
how to pick up my new changes at this point. I think eventually I did

import X

and that sort of started me back from the beginning, with my changes.
But is there a way to continue to use a from statement instead, and then
reload your changes?
Mar 2 '06 #1
3 2179
On Thu, 2 Mar 2006, John Salerno wrote:
I understand that after you import something once, you can reload it to pick
up new changes. But does reload work with from statements? I tried this:

from X import *

and then did my testing. I changed X and tried to reload it, but that didn't
seem to work. I figure the reason is because the module itself doesn't exist
as an object, only its names do. But I couldn't figure out how to pick up my
new changes at this point. I think eventually I did

import X

and that sort of started me back from the beginning, with my changes. But is
there a way to continue to use a from statement instead, and then reload your
changes?


"reload" is an ordinary procedure that takes a module as an argument. When
you use a "from X import *" statement, "X" is not imported, so you have no
module object to pass to "reload". In addition, even if you do import "X"
and reload it, you won't update your bindings; you'll still have to do
"from X import *" again to update any names imported from X before.

So, to make a long story short, you have to do something like:

import X
reload(X)
del X # to keep your namespace clean
from X import *

In general, "from X import *" should be avoided anyway, for reasons that
have been discussed many times in the past. The annoyance with reloading
is just one more reason. Better to just use "import X" in the first place.

--
.:[ dave benjamin -( ramen/sp00 )- http://spoomusic.com/ ]:.
"one man's constant is another man's variable" - alan perlis
Mar 2 '06 #2
Dave Benjamin wrote:
In general, "from X import *" should be avoided anyway, for reasons that
have been discussed many times in the past. The annoyance with reloading
is just one more reason. Better to just use "import X" in the first place.


Thanks. I kind of figured it's better to use import instead of from
anyway, but I was following along with some examples that use from
(despite the fact that earlier in the book they even say that from is
problematic and you should use import instead!) :)
Mar 2 '06 #3
On Thu, 2 Mar 2006, John Salerno wrote:
Dave Benjamin wrote:
In general, "from X import *" should be avoided anyway, for reasons that
have been discussed many times in the past. The annoyance with reloading is
just one more reason. Better to just use "import X" in the first place.


Thanks. I kind of figured it's better to use import instead of from anyway,
but I was following along with some examples that use from (despite the fact
that earlier in the book they even say that from is problematic and you
should use import instead!) :)


No problem. I stand by my original advice, but there is one semi-oneliner
that you might find useful:

reload(__import __('X')); from X import *

You could keep that in your clipboard and paste it into the interpreter
when you need to reload.

--
.:[ dave benjamin -( ramen/sp00 )- http://spoomusic.com/ ]:.
"one man's constant is another man's variable" - alan perlis
Mar 2 '06 #4

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

Similar topics

2
1814
by: Phipps Xue | last post by:
Dear all, I wanna reload a script module after it's modified so that I can use the new function immediately. Is there any help for that? It's appreciated very much if any example provided! TIA
0
1311
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...
2
16786
by: Wolfgang.Stoecher | last post by:
Hi, another beginner's question: when I import a module, change the corresponding textfile and try to import the same module again, it looks like the internal code does not change (the source-line in an error-message is up to date, however!). Also del module before import and deleting the .pyc file do not help! How to realize a fast edit-try-cycle? (btw: I am using python 2.2 under windows) thanks in advance!
2
1735
by: beliavsky | last post by:
Near the beginning of file test_matrix.py from scipy are the lines import scipy.base reload(scipy.base) from scipy.base import * del sys.path Could someone please explain why the first two lines were included? A similar script I wrote works fine without them. Also, what is the purpose of the "del" line? (I understand the mechanics of what "del"
49
3909
by: Martin Unsal | last post by:
I'm using Python for what is becoming a sizeable project and I'm already running into problems organizing code and importing packages. I feel like the Python package system, in particular the isomorphism between filesystem and namespace, doesn't seem very well suited for big projects. However, I might not really understand the Pythonic way. I'm not sure if I have a specific question here, just a general plea for advice. 1) Namespace....
6
2273
by: HMS Surprise | last post by:
I imported a set of functions from a file I wrote to interpreter shell: from myFile import * Now if I change functions in this file how can I make python forget it so I can force a fresh import? thanx,
0
954
by: Gary Herron | last post by:
Dan Yamins wrote: Because loading (and reloading) assigns values to variables (called binding a value in Python), but does not go on a hunt to find variables to *unbind*. Once a variable is bound to a value, it stays bound until something unbinds or rebinds it, and module loading does no such thing. But in fact you did *not* delete the module from memory. A module
0
1099
by: Gabriel Genellina | last post by:
En Fri, 13 Jun 2008 20:01:56 -0300, Dan Yamins <dyamins@gmail.com> escribió: Note that if you execute dir() at this point, you'll see the Operations name, *not* Operations.archive. The statement "import Operations.archive" first tries to locate and load a module named Operations - and *that* name is added to the current namespace, not Operations.archive (which is an invalid name by itself).
0
1485
by: Gabriel Genellina | last post by:
En Sat, 18 Oct 2008 05:52:04 -0300, Stef Mientki <stef.mientki@gmail.com> escribió: Why don't you let the caller tell you its own location, using __file__? The above code is too much magic for me. Yes.
0
8203
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
8146
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
8647
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
8592
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...
0
7121
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6097
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
5550
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();...
0
4063
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4141
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.