getting started, .py file

N

nibiery

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
 
S

Steve Holden

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
 
P

Peter Hansen

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
 
I

Ingrid

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
 
I

Ingrid

I found it! I needed to set sys.path ("sys.path.append("c:\\documents
and settings\\my documents\\ingrid")")

Ingrid
 
S

Steven D'Aprano

Ingrid said:
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.
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top