Re: [Tutor] Very basic Python question

2009-06-27 Thread Dave Angel

Daniel Sato wrote:



Hi,

Let me preface this by saying that I purchased O'Reilly's "Learn Python"
yesterday and have no programming experience (I am a photographer by trade)
except for a semester of what I think was BASIC on some old apple back in
elementary school (circa 1992).

I am not sure what details are relevant, so I will try and include as much
as possible.  I have a MacBook Pro running Mac OSX 10.5.6.  I recently dl'ed
MacPython 2.5, which installed Python 2.5.4 on my system.

When I am in the terminal, I can run a module by typing python
fullpath/to/script.py

However, when I enter Python from the terminal, by typing python, I can no
longer import items in this way unless the .py file is in my user folder
/Users/Me.  How can I change my settings so that I can import .py files from
a separate directory such as /Users/Me/Documents/PyMods?

Thank you.

-daniel sato

  
(Note, I'm using Python2.6 on Windows, so I may not get this quite 
right.  But it'll be close)
When Python does an import, it has a search path to use, very similar to 
the way the shell uses the 'path' variable.  This search path  may be 
examined and modified, assys.path


The interpreter knows how to find the modules and packages it was 
installed with, but not how to find an arbitrary module you just wrote.  
So you can either put your module in one of the standard places, or add 
its actual location to the sys.path.  Normally while you're 
experimenting with the interpreter, you want to do the latter.


Actually, the interpreter looks one other place for an import, the 
current working directory.  So if you set the cwd to the location of the 
script.py, it should be able to find it and anything else in the same 
directory.


Naturally, if you have more than one interpreter, you'll want to be able 
to load the proper one.  I keep shell scripts in my path called python26 
and python31 for the versions of python I normally use.  If I were 
running a system with a default version, I'd let the script python load 
that one.


So you'd want to add a shell script into your path called python25 (once 
per installation).


Then when you start an interactive session, you use

cd  /fullpath/to/
python25

import script
import sys
sys.path

::Advanced techniques, for special circumstances:::
Now, there are some differences between python25, python26, and 
python31.  So the following may not be quite right, since I don't use 
2.5 any more.  But you can add your own directories to the intiial 
sys.path using the pythonpath variable.  And you can modify that 
variable interactively, or with any python script you *can* load.  So if 
you want to have several directories of python scripts to be available 
in a single session you can use either of those approaches.



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


Re: [Tutor] Very basic Python question

2009-06-27 Thread Kent Johnson
On Sat, Jun 27, 2009 at 1:16 AM, Luke
Paireepinart wrote:

> You should try uninstalling MacPython.  Macintosh computers come with Python
> preinstalled and you should try to use that version instead.  Probably what
> happened was that, when installing the other version of Python, you messed
> up some of the references for the paths of libraries and such.

No, that is not necessary. It's fine to install a more modern Python.
Daniel just needs to understand and perhaps modify the Python search
path, as Dave Angel explained.

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


[Tutor] Converting a Py2exe Script to a Windows Installer

2009-06-27 Thread Paras K.
I have been writing many scripts using python and then making them a
standalone script using Py2exe. I have the source code for the script.

What I am trying to do is, take this script and make it into a windows
installer and have a shortcut within the All Programs or on the desktop.

Any tutorials or sites out there that will show me how to do this?

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


[Tutor] character counter

2009-06-27 Thread julie
Hello,

I need help with the following problem:

*Write a loop that reads each line of a file and counts the number of lines
that are read until the total length of the lines is 1,000 characters. Use a
break statement to make sure that you don't continue reading the file once
the 1,000 characters are read.

I figured out how to open a file, count and print the lines, however I
cannot figure out or find online ...or anywhere else how to count characters
in a file. This is what i have so far:

file = open("/Users/meitalamitai/Documents/Computer
Science/Python/Homework/Lorem_Ipsum.py")
lines = 0
for line in file:
lines=lines+1
print '%r has %r lines' % ("Lorem_Ipsum.py", lines)
   if char >= 1000:
break  *

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


Re: [Tutor] character counter

2009-06-27 Thread Richard Lovely
2009/6/27 julie :
> Hello,
>
> I need help with the following problem:
>
> Write a loop that reads each line of a file and counts the number of lines
> that are read until the total length of the lines is 1,000 characters. Use a
> break statement to make sure that you don't continue reading the file once
> the 1,000 characters are read.
>
> I figured out how to open a file, count and print the lines, however I
> cannot figure out or find online ...or anywhere else how to count characters
> in a file. This is what i have so far:
>
> file = open("/Users/meitalamitai/Documents/Computer
> Science/Python/Homework/Lorem_Ipsum.py")
> lines = 0
> for line in file:
>     lines=lines+1
> print '%r has %r lines' % ("Lorem_Ipsum.py", lines)
>    if char >= 1000:
>     break
>
> Thanks!
>
>
> ___
> Tutor maillist  -  tu...@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
In python, we don't explictily count, unless we absolutely have to.
Instead, in your situation, we measure the length of things.  Length
is what you need to be searching for.  Specifically, finding the
length of a string.

-- 
Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
www.theJNP.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] character counter

2009-06-27 Thread Emile van Sebille

On 6/27/2009 6:42 AM julie said...

Hello,

I need help with the following problem:

*Write a loop that reads each line of a file and counts the number of 
lines that are read until the total length of the lines is 1,000 
characters. Use a break statement to make sure that you don't continue 
reading the file once the 1,000 characters are read.


I figured out how to open a file, count and print the lines, however I 
cannot figure out or find online ...or anywhere else how to count 
characters in a file. 


Maybe it helps to know that each line has a length you can determine using:

   linelen = len(line)

Emile




This is what i have so far:

file = open("/Users/meitalamitai/Documents/Computer 
Science/Python/Homework/Lorem_Ipsum.py")

lines = 0
for line in file:
lines=lines+1
print '%r has %r lines' % ("Lorem_Ipsum.py", lines)
   if char >= 1000:
break  *

Thanks!




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


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