469,328 Members | 1,281 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

getting started, .py file

I am just getting started with Python, and I think I may be thinking
about it wrong. I'd like to be able to work interactively with some
code that I've got in a file. The only interpreted language I have much
experience with is Tcl/Tk, and in that I would use "source file.tcl" in
the console to load my source. Is there a similar command in python? I
know I can run my file as a script, but since I'm just experimenting
with how the language works, I want to have more flexibility to be able
to interactively check the contents of variables and define one piece
at a time. Am I missing something obvious, or am I not thinking about
Python properly?

Thanks,
Ingrid

Feb 20 '06 #1
6 1846
python -i source_file.py
will do what you want.

-Sam

Feb 20 '06 #2
ni*****@hotmail.com wrote:
I am just getting started with Python, and I think I may be thinking
about it wrong. I'd like to be able to work interactively with some
code that I've got in a file. The only interpreted language I have much
experience with is Tcl/Tk, and in that I would use "source file.tcl" in
the console to load my source. Is there a similar command in python? I
know I can run my file as a script, but since I'm just experimenting
with how the language works, I want to have more flexibility to be able
to interactively check the contents of variables and define one piece
at a time. Am I missing something obvious, or am I not thinking about
Python properly?

Yes. If your code is in "mycode.py" then start up the interactive
interpreter and enter

import mycode

at the ">>>" prompt. You will now find that names you have defined in
the mycode module can be referred to as mycode.this, mycode.that and so
on, allowing you to interactively play with the features of you module.

A related technique (possibly acceptable for interactive testing but
definitely *not* recommended for production use) is

from mycode import *

which puts the names directly into the importing module's (that is to
say the interactive interpreter's) namespace and allows you to refer to
them directly as this, that, and so on.

This interactive design is one on the Python programmer's secret
weapons, as it makes it so easy to try things out and be sure you have
got your Python correct.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Feb 20 '06 #3
ni*****@hotmail.com wrote:
I am just getting started with Python, and I think I may be thinking
about it wrong. I'd like to be able to work interactively with some
code that I've got in a file. The only interpreted language I have much
experience with is Tcl/Tk, and in that I would use "source file.tcl" in
the console to load my source. Is there a similar command in python? I
know I can run my file as a script, but since I'm just experimenting
with how the language works, I want to have more flexibility to be able
to interactively check the contents of variables and define one piece
at a time. Am I missing something obvious, or am I not thinking about
Python properly?


Note that one useful thing you can do if you take Steve's first approach
("import mycode") is to do "reload(mycode)" after you've made changes in
a text editor and resaved your mycode.py file. That will reload the
module from the source, picking up whatever changes you've made. Handy
for some kinds of quick experimentation... (though often just typing
stuff at the prompt and then integrating with your source after you've
got it working is a more efficient approach).

-Peter

Feb 20 '06 #4
Thanks everyone. That's exactly what I was looking for, but I still
can't seem to make it work. I've got the interpreter starting in
"C:\Program Files\Python2.4", and my code is in "C:\Documents and
Settings\Ingrid\My Documents". So, I did:
import os
os.chdir("C:\\Documents and Settings\\Ingrid\\My Documents")
from mycode import *

and I get the error message:
Traceback (most recent call last):
File "<pyshell#29>", line 1, in -toplevel-
import mycode
ImportError: No module named mycode

I'm sure I'm missing something obvious again, but I don't see anything
in the import documentation about directories.

Ingrid

Feb 20 '06 #5
I found it! I needed to set sys.path ("sys.path.append("c:\\documents
and settings\\my documents\\ingrid")")

Ingrid

Feb 21 '06 #6
Ingrid wrote:
Thanks everyone. That's exactly what I was looking for, but I still
can't seem to make it work. I've got the interpreter starting in
"C:\Program Files\Python2.4", and my code is in "C:\Documents and
Settings\Ingrid\My Documents". So, I did:
import os
os.chdir("C:\\Documents and Settings\\Ingrid\\My Documents")
from mycode import *


I see that Ingrid found the solution to this problem.
But I wonder, should Python support importing absolute
pathnames?

I've trying googling, but either my google skills are
lacking or nobody has ever thought of doing import
"C:\directory\module" before.
--
Steven.

Feb 21 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Jack Duijf | last post: by
13 posts views Thread by ThaDoctor | last post: by
7 posts views Thread by michael sorens | last post: by
1 post views Thread by =?Utf-8?B?Q29kZVNsaW5nZXI=?= | last post: by
7 posts views Thread by Mark Carter | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.