A beginner's problem...

A

Amir Dekel

Hello everyone,

First, I have to say that Python is one of the coolest programing
languages I have seen.
And now for the problem (must be a silly one):
When I import a module I have wrote, and then I find bugs, it seems that
I can't import it again after a fix it. It always shows the same
problem. I try del module but it doesn't work.
(I use Python 2.4 with the ActivePython pack (PythonWin IDE)

Solution anyone?

Amir
 
R

Robert P. J. Day

Hello everyone,

First, I have to say that Python is one of the coolest programing languages I
have seen.
And now for the problem (must be a silly one):
When I import a module I have wrote, and then I find bugs, it seems that I
can't import it again after a fix it. It always shows the same problem. I try
del module but it doesn't work.
(I use Python 2.4 with the ActivePython pack (PythonWin IDE)

Solution anyone?

reload?

rday
 
M

Marc 'BlackJack' Rintsch

When I import a module I have wrote, and then I find bugs, it seems that
I can't import it again after a fix it. It always shows the same
problem. I try del module but it doesn't work.
(I use Python 2.4 with the ActivePython pack (PythonWin IDE)

Solution anyone?

Yes -> help(reload)

Ciao,
Marc 'BlackJack' Rintsch
 
D

DogWalker

Marc 'BlackJack' Rintsch said:
Yes -> help(reload)

Ciao,
Marc 'BlackJack' Rintsch

First, save the file using the check option (Ctl+Shift+C, iirc);
Second, Fix any errors (attend to Status Bar);
Third, press Reload button in Toolbar (or type command from File Menu).
Four, assure that Status Bar indicates reload was successful.
If still doesn't load correctly, quit PythonWin and start it again.
 
C

Christian Ergh

DogWalker said:
First, save the file using the check option (Ctl+Shift+C, iirc);
Second, Fix any errors (attend to Status Bar);
Third, press Reload button in Toolbar (or type command from File Menu).
Four, assure that Status Bar indicates reload was successful.
If still doesn't load correctly, quit PythonWin and start it again.
Fifth, use the module unittest and write a test for your module. Just
run the test to check your module and fix all errors, then import it
into the larger sceme and see if everything works there. Most problems
will appear in a good test, so you will not have the reimport issiue at all.
Chris
 
J

James Martin

Try deleting the Compiled Python File that was created during import --
extension pyc. Then import again.

It seems to me (I'm a novice too) that, when you import a module, Python
automatically compiles it. Then when you import it later, the compiled
version is imported if it exists.
 
M

Mike Meyer

[Format recovered from top posting.]

James Martin said:
Try deleting the Compiled Python File that was created during import --
extension pyc. Then import again.

It seems to me (I'm a novice too) that, when you import a module, Python
automatically compiles it. Then when you import it later, the compiled
version is imported if it exists.

It compares the dates on the .py file with the .pyc file, and
recompiles the .py file if it's newer.

Doing a second import will find the module in sys.modules and not
bother looking in the file system. The solution is to reload(module)
instead of import module.

<mike
 
A

Amir Dekel

Mike said:
Doing a second import will find the module in sys.modules and not
bother looking in the file system. The solution is to reload(module)
instead of import module.

<mike
What if I import using "from module import class"? It seems to me that I
can't use reload then, or I just couldn't find the right syntax...
 
M

Mike Meyer

Amir Dekel said:
What if I import using "from module import class"? It seems to me that
I can't use reload then, or I just couldn't find the right syntax...

I'm pretty sure you'll have to import the module and reload it. "from
module import class" still puts the module in sys.modules, so trying
to import something from it again will find it in there.

<mike
 
N

Nick Coghlan

Amir said:
What if I import using "from module import class"? It seems to me that I
can't use reload then, or I just couldn't find the right syntax...

Correct. Using a non-from import is actually easier when experimenting, since
reload is easier to use.

If you have used a from-style import, you have to do this to force a reload of
the relevant class:

Py> from module import x
Py> # Do stuff
Py> import module
Py> reload(module)
Py> from module import x
Py> # We now have the updated version of x

Cheers,
Nick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Beginner's guide to Python 7
A Beginner's Doubt 11
Write a file - beginner's question 3
Void problem 1
Need Assistance With A Coding Problem 0
Javascipt problem. 22
C-API: A beginner's problem 10
Turtle.onclick problem 7

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top