Newbie questions on import & cmd line run
Terry Reedy
tjreedy at udel.edu
Thu May 17 00:17:25 EDT 2012
On 5/16/2012 9:45 PM, gwhite wrote:
> Hi,
>
> I am a newbie running the latest pythonxy (2.7.2.1)& spyder and
> python 2.7.2. I suspect my questions are mostly basic to python, and
> not specific to Spyder or iPython.
>
> Note: Up until now, I mainly used MATLAB, and thus need to de-program
> myself appropriately.
>
> I use Win7-64.
>
> I wrote the following .py file:
>
> -----------------
> #!<what is supposed to go here?>
> # Filename: newbie00.py
>
> if __name__ == '__main__':
> print 'This program was called from the \
> system command line.'
> print __name__ + '.py'
> else:
> print 'This program was imported on the \
> Python command line.'
> print __name__ + '.py'
>
> -----------------
>
> If I run from the system (win cmd) command, I get:
>
> C:\engineer\engruser\python>python newbie00.py
>
> This program was called from the system command line.
> __main__.py
>
> -----------------
> If I hit the run button in Sypder, I get (in the iPython command
> console):
>
> In [70]: runfile(r'C:\engineer\engruser\python\newbie00.py', wdir=r'C:
> \engineer\engruser\python')
> This program was called from the system command line.
> __main__.py
>
>
> -----------------
> If I import on the iPython command, I get:
>
> In [71]: import newbie00
> This program was imported on the Python command line.
> newbie00.py
>
> -----------------
> If I import *again* on the iPython command, I get:
>
> In [72]: import newbie00
>
> In [73]:
>
> <nothing that I can see>
Read the doc for the import command. It only runs code when needed to
create the module to import. When you import again, you re-import the
existing module.
> If I hit the run button (again) in Sypder, I get (in the iPython
> command console):
>
> In [73]: runfile(r'C:\engineer\engruser\python\newbie00.py', wdir=r'C:
> \engineer\engruser\python')
> UMD has deleted: newbie00
This output is Spyder specific. When you run from an Idle window, it prints
========================== RESTART ============================
which means that the user main namespace is reset. Idle usually does
this by starting a new user subprocess (and terminating the old one),
but the alternative would be to try to clean the existing subprocess by
reversing and resetting everything that was done. Wiping out everything
and restarting is easier ;-).
> 1. If running from the system command line, or the Sypder "run"
> button, "__name__" is "__main__" rather than "newbie00", as seen
> above. [how to get latter]
# Running in 3.3 Idle from edit window: tem.py
print(dir())
print(__file__)
# output in shell window
['__builtins__', '__doc__', '__file__', '__name__', '__package__']
F:\Python\mypy\tem.py
os.path.something() can isolate the last part in a cross-platform manner.
> 2. In python, there seems to be a distinction between running
> something as if it is a system command of "C:\...>python myPyFile.py"
You use dos syntax in the dos command prompt window.
> compared to simply entering that same ".py" file name directly on the
> python console command line.
You use python syntax in the interactive python console. The Idle Shell
closes simulates that. Other shells add extra syntax that only works in
the specific shell.
> In fact, the latter does not work unless
> the somewhat lengthy ">>> runfile(r'C:\... wdir=r'C:\...) stuff is
> entered (in iPython). (I mean, my old MATLAB habit of simply entering
> ">> mfilename" on the command line seems to be quite wrong in python.)
I believe runfile is ipython (?) specific.
>>> exec(open("F:\\Python\\mypy\\tem.py").read())
['__builtins__', '__doc__', '__file__', '__name__', '__package__',
'__warningregistry__']
F:\Python\mypy\tem.py
It is relatively unusual to run a file in the main space rather than
importing it.
> 5. I think #4 implies an import can be removed. (Yes/No?)
Yes, but it is not a newbie thing to do, and it often does not do what
people expect or want. My advice: forget about it.
> I think I saw someplace where a .pyc file is created on an initial run
> and subsequently run instead of the .py.
A .pyc file is only generated when the .py is imported. But this is
unreleated to the above and a CPython-specific behind-the-scenes time
optimization you should not worry about.
> here, but if related, I guess an auxiliary question is how to easily
> force the .py to run rather than the .pyc?
You do not want to do that. If the .py is changed, the .pyc is
regenerated on the next import.
> 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.
Look at the trace module. Of course, you distribute files, not
functions. There are various packaging programs that do a module import
trace rather than function call trace.
--
Terry Jan Reedy
More information about the Python-list
mailing list