473,890 Members | 1,320 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 2054
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:\Documen ts and
Settings\Ingrid \My Documents". So, I did:
import os
os.chdir("C:\\D ocuments and Settings\\Ingri d\\My Documents")
from mycode import *

and I get the error message:
Traceback (most recent call last):
File "<pyshell#2 9>", 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.appe nd("c:\\documen ts
and settings\\my documents\\ingr id")")

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:\Documen ts and
Settings\Ingrid \My Documents". So, I did:
import os
os.chdir("C:\\D ocuments and Settings\\Ingri d\\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\m odule" before.
--
Steven.

Feb 21 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2548
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 started sending out the content of the page. Web content is always delivered with a few headers at the top, ending with a blank line. For example, a web page might start like this:
2
3039
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 flexibility. Please help me with your suggestion's about the best way accomplish this or do you know of any examples that I could review or download. Thanks in advance! Norris
6
2189
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 Microsoft Developement Enviroment. Thanks,
13
1778
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" on the screen. Greetings Tobias
7
8443
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): if (!System.Diagnostics.EventLog.SourceExists("MySource")) { ... But received this error when attempting to start the service: System.Security.SecurityException was unhandled Message: The source was not found, but some or all event logs could...
1
4748
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 a few questions. Some getting started with Hyper-V assumptions. Please tell me if I am off base here. 1. Any recent Intel quad core processor has the needed x64 architecture with hardware assisted virtualization and data execution protection...
7
1624
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 Leopard, and I'm trying to do the same thing. I'm fairly experienced with Python, but new to OS X. I have installed FinderPop, which lets me add scripts and make them accessible via Contextual Menus, but it is not as easy as the Nautilus way. ...
4
2437
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 environment and I need to be able to prevent users from creating new databases or opening existing ones using that page. Thanks!
3
14729
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 samples for 3rd semester\visual c++ project samples\polygon drawer\dialog.h(4) : fatal error C1083: Cannot open include file: 'afxwin.h': No such file or directory 1>DrawerApp.cpp
0
9819
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10811
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10919
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10461
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8015
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7169
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6041
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4674
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 we have to send another system
2
4270
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.