Finding Module Dependancies

M

mwt

When I'm rewriting code (cutting and pasting pieces from earlier
modules), is there a quick way to determine if I have imported all the
necessary modules? I am used to working in Java, where the compiler
will tell you if you haven't imported everything, and also Eclipse,
which has the handy "organize imports" feature. This is not the case in
Python, since it's not compiled, of course, and also running it might
not insure you've got all the imports, unless you go through every
possible usage scenario -- which in some cases is quite a few, and
could take a long time.

So what I'm looking for is a command, like "check dependencies" or
something, which will list all the modules needed for a source module
to run.
 
L

Larry Bates

mwt said:
When I'm rewriting code (cutting and pasting pieces from earlier
modules), is there a quick way to determine if I have imported all the
necessary modules? I am used to working in Java, where the compiler
will tell you if you haven't imported everything, and also Eclipse,
which has the handy "organize imports" feature. This is not the case in
Python, since it's not compiled, of course, and also running it might
not insure you've got all the imports, unless you go through every
possible usage scenario -- which in some cases is quite a few, and
could take a long time.

So what I'm looking for is a command, like "check dependencies" or
something, which will list all the modules needed for a source module
to run.
If you are an accomplished Eclipse user (I'm not), you should look at
the Python plug-in for Eclipse.

Remember that Python is so dynamic that you can build dependencies
during program execution and import them on-the-fly. So a dependency
checker would always be incomplete. You always need to write unit
tests.

-Larry Bates
 
J

John Machin

When I'm rewriting code (cutting and pasting pieces from earlier
modules)

Instead of propagating multiple copies of source code, consider
refactoring those modules so that top-level functions and classes can be
used in other modules.
is there a quick way to determine if I have imported all the
necessary modules? I am used to working in Java, where the compiler
will tell you if you haven't imported everything,

That's a clever design. It would be even better if it just shut up and
did the imports for you :)
and also Eclipse,
which has the handy "organize imports" feature. This is not the case in
Python, since it's not compiled, of course, and also running it might
not insure you've got all the imports, unless you go through every
possible usage scenario -- which in some cases is quite a few, and
could take a long time.

This is called "testing". Yes, it could take a long time.
So what I'm looking for is a command, like "check dependencies" or
something, which will list all the modules needed for a source module
to run.

Consider pychecker and pylint. I haven't tried pylint, but pychecker
does what you want and a whole lot more:

C:\junk>type miss_import.py
# need to import re, but forgot
def my_isdigit(s):
return bool(re.match(r"\d+", s))

C:\junk>pychecker miss_import

C:\junk>c:\python24\python.exe
c:\python24\Lib\site-packages\pychecker\checker.py miss_import
Processing miss_import...

Warnings...

miss_import.py:3: No global (re) found

C:\junk>
 
M

mwt

John said:
This is called "testing". Yes, it could take a long time.

Thanks for the clarification. ;) Actually, I've done hellish amounts
of testing on these code pieces, which is why I don't want to have to
do it all over again just to check the imports.

Consider pychecker and pylint. I haven't tried pylint, but pychecker
does what you want and a whole lot more:

C:\junk>type miss_import.py
# need to import re, but forgot
def my_isdigit(s):
return bool(re.match(r"\d+", s))

C:\junk>pychecker miss_import

C:\junk>c:\python24\python.exe
c:\python24\Lib\site-packages\pychecker\checker.py miss_import
Processing miss_import...

Warnings...

miss_import.py:3: No global (re) found

C:\junk>

That sounds really useful. I'll check it out. Thanks!
 
M

mwt

Wow! Just ran pychecker on a couple of modules. I'm blown away by how
much information I'm getting. Thanks again!
 
M

mwt

Wow! Just ran pychecker on a couple of modules. I'm blown away by how
much information I'm getting. Thanks again!
 
D

Dennis Lee Bieber

So what I'm looking for is a command, like "check dependencies" or
something, which will list all the modules needed for a source module
to run.

Problem is... You can have things like...

{pseudo-code}
if BossIsWatching:
import HisModule as M
elif LunchTime:
import MyModule as M
elif 00:00:00 < TimeNow < 12:00:00 : #remember, pseudocode
import MorningModule as M
elif 12:00:00 < TimeNow < 24:00:00 :
import EveningModule as M

M.LookBusy()


Note: There is a flaw in that block of code -- what module gets
loaded at time 12:00:00? And how would you determine that you are
missing a module for that situation?

And what happens if you have a

from XYZ import *

and M.LookBusy() is something where "M" came from the *?
--
 
R

robert

Larry said:
Remember that Python is so dynamic that you can build dependencies
during program execution and import them on-the-fly. So a dependency
checker would always be incomplete. You always need to write unit
tests.

The Pychecker will usually see also missing on-the-fly imports and most
of that like.
You'd need to do tricky exec stuff to trick him - and even then he can
warn for smelling style of code.
You'd have to add upon warnings a #$pycheck_no to all such smelling
lines in order to confirm, that you really know what you are doing.

To my experience you'd usually not get the written tests dense enough
(at reasonable costs) to look into all branches of execution flow.

All 5 things are needed for efficiency :
(frequency of application decreasing the list down)

* auto-postmortem debugging (dev) ( auto-postmortem report (dist))
* running written tests
* code checks
* walking major execution branches by manual tests
* beta cycling

-robert
 

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

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top