473,503 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2026
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2518
by: Philip Ronan | last post by:
OK, here's my 2p worth: === Q. Why am I getting the error message 'Headers already sent'? A. PHP produces this error message when you try to set a header for a web page after you have already...
2
3011
by: N. Graves | last post by:
I'm planning to develop a process to import data from an external flat file to a new table automatically. As usual I'm having a hard time getting started on the solution because of the Access's...
6
2173
by: Jack Duijf | last post by:
Hello, I am looking for a person in The Netherlands that is willing to help me getting started with Vb.net. Please send a message to jack@aicn.nl if you can help me getting started with the...
13
1737
by: ThaDoctor | last post by:
I would like to get started with programming in C, but I cant get it to work, I do not need any compiler or such. Only a basic explanation of the first program Hello World that print "Hello World"...
7
8415
by: michael sorens | last post by:
(1) I tried to use what seems like a standard line (from Walkthrough: Creating a Windows Service Application in the Component Designer at http://msdn2.microsoft.com/en-us/library/zt39148a.aspx): ...
1
4717
by: =?Utf-8?B?Q29kZVNsaW5nZXI=?= | last post by:
I plan to build my own 2008 Server/Hyper-V system and will not be using one of the tested Dell or HP systems from the release notes and could use some pointers as to my assumnptions and answers to...
7
1605
by: Mark Carter | last post by:
One thing I really liked about Ubuntu was that Nautilus allowed you to add scripts to a directory which could be accessed via the RMB. It was a very simple thing to do. I've recently switched to...
4
2417
by: evenlater | last post by:
Anybody know how to prevent the annoying Access 2007 "Getting Started with Access" page from showing up when the database is closed using DoCmd.Quit? My database is used in a terminal server...
3
14674
lifeisgreat20009
by: lifeisgreat20009 | last post by:
I am getting the following error :- 1>------ Build started: Project: Polygon Drawer, Configuration: Debug Win32 ------ 1>Compiling... 1>Dialog.cpp 1>g:\my subjects\3rd semester\project...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.