Newbie questions on import & cmd line run

alex23 wuwei23 at gmail.com
Thu May 17 00:54:37 EDT 2012


On May 17, 11:45 am, gwhite <gwh... at ti.com> wrote:
> 1.  If running from the system command line, or the Sypder "run"
> button, "__name__" is "__main__" rather than "newbie00", as seen
> above.
>
> So, how would I get the file name newbie00.py in these two noted
> cases?

You can get it from the file name:

    import os.path
    name = os.path.split(__file__)[-1]

However, you might find it better in the long term to always separate
your code into import-only modules and execute-only scripts. It avoids
this issue and others.

> 2.  Is there a shortened syntax of running a .py from the python command
> prompt, if not using a Spyder "run" button?  Or should I always run as
> if from the system prompt?  That is, dispense with the MATLAB-like
> "run from MATLAB/python command line" bias I may be holding.

Generally, the correct way of running Python code from the interpreter
is 'import <module>'. You might find this pattern useful:

In your module:

    def main(): # code goes here

    if __name__ == '__main__': main()

Then from the interpreter:

    import mymodule; mymodule.main()

> 3.  In injecting my old MATLAB bias of running via the command line
> ">> mfilename", I tried a tweak of  ">>>import newbie00".  That "sort
> of" worked, but only the first time.
>
> Why did the subsequent run of ">>>import newbie00" print nothing?  I'm
> just trying to understand how python works.

The import mechanism only imports a module once, as all files use the
same module instance (they're effectively singletons). This happens
with repeated imports in one module as well as across various modules
during a single execution. So re-importing a module does nothing; to
force a re-import, you can use the reload() function, although that
doesn't guarantee updating all references. For example:

    import mymodule
    from mymodule import myfunc

    # modify myfunc code externally

    reload(mymodule)
    myfunc() # original reference
    mymodule.myfunc()  # newly modified function

I don't think that only-one-import is true for scripts that are run
from the command line, though. They can exist as both '__main__' and
their actual name in the module table. (Someone please correct me if
this understanding is wrong...)

> 4.  The final case shown of hitting the Spyder run button included
> this:
>
> UMD has deleted: newbie00
>
> What does that mean?  I noted that after this "automatic" deletion, I
> could do the ">>>import newbie00" once again and get the print.  (I
> did not show that above.)

Spyder provides a convenience feature to force the reimport of user-
defined modules, like your newbie00. After execution, it appears to
drop all references to your module, forcing a garbage collection. As
its no longer loaded, a subsequent import works.

> 5.  I think #4 implies an import can be removed.  (Yes/No?)  I am not
> sure why that would be desired, but I will ask how to remove an
> import, or to "refresh" the run, of that is the appropriate question.

reload(<module>)

However, the only way to guarantee you've updated all references
correctly is to close the interpreter and re-start it. For this kind
of development process of modifying code and seeing the changes, it's
probably better to look into writing tests instead. These will always
be run in isolation, so you're guaranteed of having the correct
environment each time.

> I think I saw someplace where a .pyc file is created on an initial run
> and subsequently run instead of the .py.  I'm not sure if that applies
> here, but if related, I guess an auxiliary question is how to easily
> force the .py to run rather than the .pyc?

A .pyc file won't be created for .py files that are run directly, only
for those that are imported. You really shouldn't need to worry about
this, though. It's an implementation detail that isn't influencing the
issues you're seeing. However, if you do ever need to do it, you can
stop .pyc files by passing the -B flag to the interpreter, or by
setting the environment variable PYTHONDONTWRITEBYTECODE.

> 6.  Perhaps peripherally related to getting a running script/function/
> module name, is getting a "call listing" of all the functions (and
> modules) called by a .py program.  How would I get that?  I only ask
> as it comes in handy if one distributes a program.  I mean, you only
> give people what they actually need.

Perhaps this might be of use: http://pycallgraph.slowchop.com/

Hope this helps.



More information about the Python-list mailing list