G
gwhite
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>
-----------------
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 program was called from the system command line.
__main__.py
-----------------------------------
Some questions:
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? I mean, what other function do I use to get it? (This
functionality is something that occasionally came in handy back in my
m-file writing days. Perhaps I am wrong in anticipating such an
occasional need, but I would not know that yet.)
2. In python, there seems to be a distinction between running
something as if it is a system command of "C:\...>python myPyFile.py"
compared to simply entering that same ".py" file name directly on the
python console command line. 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.)
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.
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.
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.)
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.
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?
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.
-----------
Advance thanks to any one who answers any of my questions. I am sure
they appear from misguided to rudimentary in character. I'm don't
claim to be a programmer; I did okay with MATLAB. I'm trying to
bootstrap here, and appreciate any help.
Thanks!
Greg
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>
-----------------
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 program was called from the system command line.
__main__.py
-----------------------------------
Some questions:
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? I mean, what other function do I use to get it? (This
functionality is something that occasionally came in handy back in my
m-file writing days. Perhaps I am wrong in anticipating such an
occasional need, but I would not know that yet.)
2. In python, there seems to be a distinction between running
something as if it is a system command of "C:\...>python myPyFile.py"
compared to simply entering that same ".py" file name directly on the
python console command line. 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.)
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.
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.
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.)
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.
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?
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.
-----------
Advance thanks to any one who answers any of my questions. I am sure
they appear from misguided to rudimentary in character. I'm don't
claim to be a programmer; I did okay with MATLAB. I'm trying to
bootstrap here, and appreciate any help.
Thanks!
Greg