Re: [Tutor] Runing a Python program

2006-05-13 Thread Evans Anyokwu



There's a simple way you can add your directory to 
the execution path. 
try this 
>>> 
sys.path.append(r'C:\python24\myPythonFiles')
now, you can import your file with the import 
command
>>> import yourFile
 
Note: This is only a temporary solution, when you 
close the interpreter, it will need to be appended again.
 
Hope that help.
 

  - Original Message - 
  From: 
  Henry Dominik 
  To: Tutor 
  Sent: Friday, May 12, 2006 9:24 PM
  Subject: [Tutor] Runing a Python 
  program
  
  Hello people,
   
  As a new python programmer, I created a directory 
  in 'C:\python24\myPythonFiles',
  and added a simple python under the myPythonFiles 
  directory; but when I tried running it on the Python Shell, I got the 
  following error.
   
  >>> import 
  myPythonFiles.readOut
   
  Traceback (most recent call last):  File 
  "", line 1, in -toplevel-    import 
  mypythonFiles.readOut
    ImportError: No module named 
  myPythonFiles.readOut
   
  >>> 
  How do I run a program that is placed on its own 
  folder/directory?
   
  Thanks
   
  Henry
  
  

  ___Tutor maillist  
  -  
Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Runing a Python program

2006-05-13 Thread Kent Johnson
Henry Dominik wrote:
> Hello people,
>  
> As a new python programmer, I created a directory in 
> 'C:\python24\myPythonFiles',
> and added a simple python under the myPythonFiles directory; but when I 
> tried running it on the Python Shell, I got the following error.
>  
>  >>> import myPythonFiles.readOut

To be able to import a module, the directory containing the module must 
be in sys.path. sys.path is just a list of directory paths. The Python 
runtime searches each of these directories for your module.

So one thing you can do is make sure your module is in a directory that 
is in sys.path. A couple of possibilities are the current working 
directory and the site-packages directory.

On my computer (Win2K) Python puts the current working directory in 
sys.path. (I'm not sure this happens on Linux.) You can see this if you 
print sys.path; it is the empty string that starts the list. So I often 
cd to the directory containing a program before starting Python. Then I 
can import modules from that directory.

For modules you want to be able to use from several programs, you can 
put them in C:\Python24\Lib\site-packages. This directory is always 
added to sys.path and it is intended as a place to install extra modules 
and packages. Most third-party modules will install to site-packages.

Alternately you can modify sys.path to include the dir you want. There 
are several ways to do this. One way, as Evans showed, is to change it 
at runtime by appending a new path. This is fine for temporary changes 
but not very convenient in the long run. Another possibility is to edit 
the environment variable PYTHONPATH and add your dir to it. You can also 
create a .pth file in site-packages that contains the path to the dir to 
add to sys.path.

You can find more info here:
http://docs.python.org/tut/node8.html#SECTION00811
http://docs.python.org/lib/module-site.html

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Runing a Python program

2006-05-13 Thread Alan Gauld
Hi Henry,

> As a new python programmer, I created a directory in 
> 'C:\python24\myPythonFiles', and added a simple python 
> under the myPythonFiles directory; but when I tried 
> running it on the Python Shell, I got the following error.
>
>>> import myPythonFiles.readOut
>  ImportError: No module named myPythonFiles.readOut

Ok, the first thing to say is that you are not running the 
program but importing it, there is a difference.

To run a Python program in Windows either double click it 
in Windows Explorer (it should have a snake icon to show 
that the association is set up correctly) or at a DOS 
command prompt (or the Start->Run dialog) type

C:\> python C:\python24\myPythonFiles\readOut.py

In either case you may find the program runs so fast you 
can't see the output. In that case add a final line like:

raw_input("Hit ENTER to quit")

When you import a module it does indeed execute
the code in that module and so might appear to run 
the program, but there is one important distinction
(which you may not have come across yet!) You can 
put a clause into the module like:

if __name__ == "__main__":
 # some code here

and the code under the if will only be executed when the 
file is run as a program, it will NOT be executed when 
the file is imported as a module. (This is a powerful feature 
that greatly eases the task of writing reusable code 
in Python.)

Now the second thing to note is that your import syntax 
is wrong.

To make the files in your directory visible toi the import 
command you need to modify the value in sys.path.
There are a number of ways to do this, the one that I 
use is to create a PYTHONPATH environment variable
(MyComputer->Properties->Advanced->Environment Variables)
which points to all the folders where I keep Python code, 
in the same way that the DOS PATH variable points to 
my executable files.

HTH,

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Database topic now fixed

2006-05-13 Thread Alan Gauld
The mistakes in my databae topic have now been rectified.
I have no idea how I managed to post the file without testing 
that section of code. And unfortunately it's one of the areas 
where SqlLite SQL syntax varies from the Oracle syntax that 
I'm most familiar with. But that's only a partial excuse since 
my code wasn't actually valid Oracle code either, it was just 
wrong!

Hopefully all errors are now removed

apologies once again,

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Runing a Python program

2006-05-13 Thread w chun
> On my computer (Win2K) Python puts the current working directory in
> sys.path. (I'm not sure this happens on Linux.)

yes it does, on any unix-flavored system (Linux, FreeBSD, MacOS X,
Solaris, etc.).

since we're on the topic, there is another attribute in the sys
module, sys.modules that shows you all the imported (and loaded)
modules and where they live in the filesystem.  it's a good way to
figure out path problems too.

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Database topic now fixed

2006-05-13 Thread Bob Gailer
Alan Gauld wrote:
> The mistakes in my databae topic have now been rectified.
> I have no idea how I managed to post the file without testing 
> that section of code. And unfortunately it's one of the areas 
> where SqlLite SQL syntax varies from the Oracle syntax that 
> I'm most familiar with. But that's only a partial excuse since 
> my code wasn't actually valid Oracle code either, it was just 
> wrong!
>
> Hopefully all errors are now removed
This is my first reading of that topic. Thank you for all the effort you 
put into this, as well as your willingness to admit and fix problems. If 
only Microsoft had that attitude.

Some observations:

1 - people familiar with other databases might be puzzled by the lack of 
column datatypes. I'd mention that sqlite allows but does not require 
types at the first introduction of CREATE TABLE,.

2 - The introduction of NULL: "NOT NULL is self explanatory, it 
indicates that the value must exist and not be NULL."

To someone completely new to SQL this is IMHO not self explanatory. This 
is the first appearance of "NULL" in the tutorial, and I deem a little 
explanation would help. Perhaps a definition of NULL then an INSERT 
statement that does not cover all columns, showing what happens when a 
NULL column is omitted and then the result of a SELECT, then showing 
what happens when a NOT NULL column is omitted (Error).

3 - "UNIQUE means that the value must be unique within the table. If it 
is not, an error results and the row will not be inserted." This is 
slightly confusing (and not totally true "unique within the table" 
really is within the column).

I'd reword it something like "an insert statement that would put a value 
in a unique column that is already in that column will raise an error 
and not insert a row"

4 - introducing WHERE. I'd first show it as WHERE x = y and then 
introduce the table prefix.

5 - following the explanation of UNIQUE under constraints, please also 
explain DEFAULT.

6- the table structure in Address Book Revised looks like it is for UK 
addresses. Consider saying this. Also consider a structure that would 
accommodate US/Canadian address also.

7 - "SELECT First,Last, Phone FROM Address WHERE First Like Dav%;" 
Shouldn't Dav% be in quotes?

HTH



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Database topic now fixed

2006-05-13 Thread Alan Gauld
Thanks Bob,

Feedback is always appreciated, its how the tutor improves.

> 1 - people familiar with other databases might be puzzled by the 
> lack of column datatypes. I'd mention that sqlite allows but does 
> not require types at the first introduction of CREATE TABLE,.

Fair point, although by definition my tutor is aimed at absolute
novices so they are less likely to know about SQL. But I will
make a comment.

> 2 - The introduction of NULL: "NOT NULL is self explanatory, it 
> indicates that the value must exist and not be NULL."

Oops, self referencing definitions are never good (unless its GNU)
I'll fix that one.

> > 3 - "UNIQUE means that the value must be unique within the table. 
> > If it
> slightly confusing (and not totally true "unique within the table" 
> really is within the column).

Quite right, I'll clarify it.

> 4 - introducing WHERE. I'd first show it as WHERE x = y and then 
> introduce the table prefix.

My general style in the tutor is to teach the most general case first
then introduce the shortcuts. Fully specified names will never go
wrong but shortened names can cause confusion in my experience.
So for this one I'lll stick with the long way first then introduce
the shoirter form.

> 5 - following the explanation of UNIQUE under constraints, please 
> also explain DEFAULT.

Fair point.

> 6- the table structure in Address Book Revised looks like it is for 
> UK addresses. Consider saying this. Also consider a structure that 
> would accommodate US/Canadian address also.

Erm, What is the difference exactly?

> 7 - "SELECT First,Last, Phone FROM Address WHERE First Like Dav%;" 
> Shouldn't Dav% be in quotes?

Oops yes, it is in my actual code but they have been dropped in the
editing somewhere.

Oh well, not quite as faultless as I hoped but better than it was! :-)

Thanks for the feedback Bob, I'll incorporate most of it in the next
upload.

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Spanish Tutorials

2006-05-13 Thread Carlos Daniel Ruvalcaba Valenzuela
Hello!

At my university we are creating a Wiki with information about the
careers and articles, one of the most important section is the Systems
Engineering, which sports a Python section :)

Spanish Python Tutorials at:

http://wikitec.itnogales.edu.mx/index.php/Python

Saludos!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Problems with Treeview

2006-05-13 Thread John CORRY








Hi,

 

I am having problems selecting a row in treeview.
 I am using Pythoncard,
Glade 2 and Python 2.4.

 

I am using the following code:-

 

combo3 = self.wTree.get_widget("treeview1")

model=gtk.TreeStore(gobject.TYPE_STRING,gobject.TYPE_STRING,gobject.TYPE_STRING,gobject.TYPE_STRING,gobject.TYPE_STRING,gobject.TYPE_STRING,gobject.TYPE_STRING,gobject.TYPE_STRING)

self.hostsmodel = model

combo3.set_model(model)

combo3.connect("row_activated", self.callback53, combo3,model)

 

 

def
callback53(self,data,combo3,data2,data3,model):

    

    view = gtk.TreeView(model)

    selection = view.get_selection().get_selected()

    

    print selection

    

    result = model.get_value(iter,0)

    

    print result

 

I get the following error:

(, None)

Traceback (most
recent call last):

  File
"C:\Documents and Settings\Johnc\Projects\project7\shopcall.py", line
1

, in callback53

    result = model.get_value(iter,0)

TypeError: iter must be a GtkTreeIter

 

What do I need to assign to iter
to make this code work?

 

Thanks,

 

John.






___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How do I create the equivalent of a Java class in Python?

2006-05-13 Thread Nathan Pinno



Hey 
all,
 
How do I create the 
equivalent of a Java class in Python? I've been looking at the reference, and 
it's been confusing to me at least.
 
Thanks,
Nathan Pinno
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Database topic now fixed

2006-05-13 Thread Alan Gauld
> Some observations:

These have now all been addressed as previously discussed.

I've also added a section at the end covering the use of execute() to
insert parameters rather than relying on format strings since the
latter can be a security risk.

Hopefully its now complete! :-)

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How do I create the equivalent of a Java class in Python?

2006-05-13 Thread w chun
> How do I create the equivalent of a Java class in Python? I've been looking
> at the reference, and it's been confusing to me at least.

can you clarify what you are looking for?  do you want to create a
class using Python, create the equivalent of a Java class using Python
via Jython, accessing a preexisting Java class from Python, or what?
you cannot simply write Java code in Python, but Python classes do get
turned into Java classes under the covers.

also, what is confusing you in the documentation?  can you send a pointer to it?

thanks,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How do I create the equivalent of a Java class in Python?

2006-05-13 Thread Danny Yoo

> How do I create the equivalent of a Java class in Python? I've been 
> looking at the reference, and it's been confusing to me at least.

You may want to look at a tutorial on classes, like:

 http://www.freenetpages.co.uk/hp/alan.gauld/tutclass.htm

Does this help?

Best of wishes!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor