Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Executing Python Scripts on Mac using Python Launcher

1,990 views
Skip to first unread message

David Thomas

unread,
Jun 25, 2012, 2:19:54 PM6/25/12
to
Hello,
This is my first post so go easy on me. I am just beginning to program using Python on Mac. When I try to execute a file using Python Launcher my code seems to cause an error in terminal, when I execute the exact same piece of code and run it in windows it seems to execute as exactly intended.
How can I make my python script to open up correctly using Python Launcher? I am running 10.7 on my Mac. I would upload a photo with the error I get but I can't seem to find the upload feature in this group.

Thanks

Benjamin Kaplan

unread,
Jun 25, 2012, 3:49:07 PM6/25/12
to pytho...@python.org
> --

You can't find the upload feature because this isn't a Google Group.
It's a Usenet newsgroup that Google Groups provides access to that's
also available as a mailing list. If you want to provide an image, use
an image host and link to the image.

As to your question, I have a few questions of my own. What version of
Python are you using? Is it the version included in Mac OS X,
installed from a python.org binary, compiled by source, or installed
through a package manager? What happens if you call the script from
the command line instead of using the Launcher application?

David Thomas

unread,
Jun 26, 2012, 1:19:45 PM6/26/12
to pytho...@python.org
I have installed Python 2.7.3 from Python.org also in Terminal it states that I have 2.7.3.
How can I execute the script from Terminal? I've tried typing python into the window and then dragging the file to terminal but I get a syntax error. Sorry I am new to Python and just want to know how I can open such a file using 10.7.

Thanks

http://www.freeimagehosting.net/ilbqt
http://www.freeimagehosting.net/r5ars

David Thomas

unread,
Jun 26, 2012, 1:19:45 PM6/26/12
to comp.lan...@googlegroups.com, pytho...@python.org
I have installed Python 2.7.3 from Python.org also in Terminal it states that I have 2.7.3.
How can I execute the script from Terminal? I've tried typing python into the window and then dragging the file to terminal but I get a syntax error. Sorry I am new to Python and just want to know how I can open such a file using 10.7.

Thanks

http://www.freeimagehosting.net/ilbqt
http://www.freeimagehosting.net/r5ars
On Monday, June 25, 2012 8:49:07 PM UTC+1, Benjamin Kaplan wrote:

Dave Angel

unread,
Jun 26, 2012, 1:37:42 PM6/26/12
to David Thomas, pytho...@python.org, comp.lan...@googlegroups.com
On 06/26/2012 01:19 PM, David Thomas wrote:
> I have installed Python 2.7.3 from Python.org also in Terminal it states that I have 2.7.3.
> How can I execute the script from Terminal? I've tried typing python into the window and then dragging the file to terminal but I get a syntax error. Sorry I am new to Python and just want to know how I can open such a file using 10.7.
>
> Thanks

Assuming python is on your PATH within that terminal, you'd normally do
something like:

cd /fullpathtoscript (the location of your script)
python myscript.py (or whatever your script is called)


If that doesn't work for you, please be specific about what happens.
Best way to do that is to copy/paste the text (showing in terminal)
into your reply here.

Also, please don't top-post. Put your new text AFTER the part you've
quoted from earlier messages.

--

DaveA

David Thomas

unread,
Jun 26, 2012, 2:09:54 PM6/26/12
to comp.lan...@googlegroups.com, pytho...@python.org, d...@davea.name, David Thomas
Hi

David Thomas

unread,
Jun 26, 2012, 2:09:54 PM6/26/12
to David Thomas, pytho...@python.org, d...@davea.name
On Tuesday, June 26, 2012 6:37:42 PM UTC+1, Dave Angel wrote:
On Tuesday, June 26, 2012 6:37:42 PM UTC+1, Dave Angel wrote:
On Tuesday, June 26, 2012 6:37:42 PM UTC+1, Dave Angel wrote:
On Tuesday, June 26, 2012 6:37:42 PM UTC+1, Dave Angel wrote:
Hi

On Tuesday, June 26, 2012 6:37:42 PM UTC+1, Dave Angel wrote:
On Tuesday, June 26, 2012 6:37:42 PM UTC+1, Dave Angel wrote:
On Tuesday, June 26, 2012 6:37:42 PM UTC+1, Dave Angel wrote:

Dave Angel

unread,
Jun 26, 2012, 2:32:53 PM6/26/12
to David Thomas, PYTHON
(You forgot to include the list on this message, but I'm including them
in my reply)

On 06/26/2012 02:07 PM, David Thomas wrote:
>
>
> On Tuesday, June 26, 2012 6:37:42 PM UTC+1, Dave Angel wrote:
>>
>> On 06/26/2012 01:19 PM, David Thomas wrote:
>>> I have installed Python 2.7.3 from Python.org also in Terminal it states
>> that I have 2.7.3.
>>> How can I execute the script from Terminal? I've tried typing python
>> into the window and then dragging the file to terminal but I get a syntax
>> error. Sorry I am new to Python and just want to know how I can open such
>> a file using 10.7.
>>>
>>>
>> <SNIP>
>>
> Thanks for the fast response how can I find out if Python is on the same
> path as Terminal?
>
> Is this located in the /usr Folder?
>
> Thanks
>

Looks like you're new to Unix as well. (the following applies to Unix,
Linux, OS/X, and with modifications, to Windows)

Terminal is a program.

PATH is an environment variable. You normally set it globally, but it's
sometimes modified by various scripts. To see what it is in a given
Terminal session, simply issue the command:

echo $PATH

To see all your environment variables, use

printenv

Anyway, PATH is a list of directories that bash (or whatever shell
you're using) will search. If you can run python with the simple command:

python

then it IS on your PATH. That means that you do NOT want to cd to the
directory of python, but to the directory where your script is located.

cd /fullpathtoscript (the location of your script)
python myscript.py (or whatever your script is called)


--

DaveA


David Thomas

unread,
Jun 26, 2012, 2:11:51 PM6/26/12
to
Thank you for the fast response. How can I tell if Python is in the same path as Terminal? Is this located in the /usr folder?

Thanks again

Benjamin Kaplan

unread,
Jun 26, 2012, 2:56:34 PM6/26/12
to pytho...@python.org
On Tue, Jun 26, 2012 at 10:19 AM, David Thomas <dtho...@me.com> wrote:
> I have installed Python 2.7.3 from Python.org also in Terminal it states that I have 2.7.3.
> How can I execute the script from Terminal?  I've tried typing python into the window and then dragging the file to terminal but I get a syntax error.  Sorry I am new to Python and just want to know how I can open such a file using 10.7.
>
> Thanks
>
> http://www.freeimagehosting.net/ilbqt
> http://www.freeimagehosting.net/r5ars

My guess would be that you're getting a SyntaxError when you run it
through the Launcher too. WIthout seeing the script, I don't know why
that would is.

I can tell you one thing you're doing wrong. Never, ever, use input()
in Python 2. If you do, all someone has to do is type in
__import__('os').remove(__file__) and stuff starts getting deleted.
"input" interprets what it reads in. Use raw_input() instead, which
always returns a string.

Hans Mulder

unread,
Jun 26, 2012, 3:16:11 PM6/26/12
to
It isn't. Terminal is an application and is located in
/Applications/Utilities . Python is an executable, and is
typically located in a "bin" directory. To find out where
it is, type

type python

at the shell prompt (that's the first prompt you get if you
open a Terminal window).

Possible answers include:

/usr/bin/python
This is the Python shipped by Apple

/Library/Frameworks/Python.framework/Versions/2.7/bin/python
This is a Python from python.org

/opt/local/bin/python
This is a Python from macports

> Is this located in the /usr folder?

If you mean /usr/bin, that's the Python that came with your Mac.
If you haven't installed any other Pythons, then yes, you must
be using that one.


Hope this helps,

-- HansM


Message has been deleted

Hans Mulder

unread,
Jun 26, 2012, 4:12:54 PM6/26/12
to
On 26/06/12 21:51:41, Dennis Lee Bieber wrote:
> On Tue, 26 Jun 2012 10:19:45 -0700 (PDT), David Thomas
> <dtho...@me.com> declaimed the following in gmane.comp.python.general:
>
>
>> http://www.freeimagehosting.net/ilbqt
>
> That's an interesting configuration...
>
> "pythonw.exe" is a version of the Python interpreter designed to NOT
> OPEN a console -- so you've got a configuration saying "open a console
> to run a no-console interpreter".

That's on Windows; on the Mac python and pythonw are identical.
In fact, they're hard links to the same file.

> Normally pythonw.exe is used with scripts having .pyw extension;
> these are scripts that use tkinter, wxPython, or other GUI system to
> create a graphical environment and don't want a console (terminal)
> window cluttering the screen when they don't use text I/O.
>
> Console based programs (.py) should be run using python.exe; adjust
> your settings.

That shouldn't matter on a Mac.

You may want to check "allow #! to override", though.

I mean, if there is a #! in a file pointing to a specific version of
python, then it's probably there for a reason. For example, the script
might use a third party module installed only in that Python install.

>> http://www.freeimagehosting.net/r5ars
>
> And this is to be expected... In Python 2.x, "input()" attempts to
> evaluate the input data, and you didn't supply anything -- hence EOF.
> For your usage, you want "raw_input()", which just grabs the next line
> of text and returns it as a string.

What he says.

> As for how to run if you've opened a console (shell [bash]) window,
> the way to run a script is to type
>
> python <path/name/to/your/script>.py <any command line arguments>

Dave Angel

unread,
Jun 26, 2012, 4:41:59 PM6/26/12
to Hans Mulder, pytho...@python.org
On 06/26/2012 03:16 PM, Hans Mulder wrote:
> <SNIP>
>
> Python is an executable, and is
> typically located in a "bin" directory. To find out where
> it is, type
>
> type python
>
> at the shell prompt (that's the first prompt you get if you
> open a Terminal window).
>
>

That's a typo. You presumably meant:

which python

--

DaveA

Hans Mulder

unread,
Jun 26, 2012, 5:48:22 PM6/26/12
to
No, I meant:

$ type python
python is /Library/Frameworks/Python.framework/Versions/2.7/bin/python

'type' is a bash builtin that tells you how bash would interpret
a command. 'which' is a separate program, which tells you how
csh would interpret a command. For a 'bash' user, 'type' is more
accurate than 'which'. For example, 'type' recognizes 'bash'
builtins.

-- HansM


Message has been deleted

Mark Lawrence

unread,
Jun 26, 2012, 7:13:42 PM6/26/12
to pytho...@python.org
On 26/06/2012 23:35, Dennis Lee Bieber wrote:
>
> (And, on Windows at least, #! lines don't do
> anything<G>)
>

New for Python 3.3 http://www.python.org/dev/peps/pep-0397/

--
Cheers.

Mark Lawrence.

Dave Angel

unread,
Jun 26, 2012, 10:38:54 PM6/26/12
to Hans Mulder, pytho...@python.org
On 06/26/2012 05:48 PM, Hans Mulder wrote:
> On 26/06/12 22:41:59, Dave Angel wrote:
> <SNIP>

> No, I meant: $ type python python is
> /Library/Frameworks/Python.framework/Versions/2.7/bin/python 'type' is
> a bash builtin that tells you how bash would interpret a command.
> 'which' is a separate program, which tells you how csh would interpret
> a command. For a 'bash' user, 'type' is more accurate than 'which'.
> For example, 'type' recognizes 'bash' builtins. -- HansM

Thanks for the information. "which" has worked on every unix-like system
I've used for 25 years. I never knew that bash had its own extension.


--

DaveA

David Thomas

unread,
Jun 27, 2012, 1:05:44 PM6/27/12
to
I have the following:

Is this why I keep getting an error using launcher? Also to open the script in terminal do I need to put the following at the beginning of my script: #!/bin/python

On python.org it states to To run your script from the Terminal window you must make sure that /usr/local/bin is in your shell search path.

How can I make sure that the Python I have installed on my Mac is in my shell search path.

http://www.freeimagehosting.net/saskk

Thanks again and I am sorry for all the questions, I am just getting started on Python

Hans Mulder

unread,
Jun 27, 2012, 2:29:38 PM6/27/12
to
On 27/06/12 19:05:44, David Thomas wrote:
> Is this why I keep getting an error using launcher?

No.

Yesterday your problem was that you tried this:

input("\n\nPress the enter key to exit")

That works fine in Pyhton3, but you are using python2
and in python2, the you must do this instead:

raw_input("\n\nPress the enter key to exit")

If you still get that error, you can either use "raw_input()"
or switch to Python3, where "input" would be correct.

If you're learning Python from a tutorial, you should
carefully read the first section of the tutorial, where
they mention whether the explains pyhton2 or python3,
and use a matching Python (or a matching tutorial).

If the tutorial doesn't say for which version it is,
then it's for pyhton2 (and several years old).

> Also to open the script in terminal do I need to put
> the following at the beginning of my script:
>
> #!/bin/python

No, that doesn't help.

There are at least three pythons on your system, but
there isn't one in /bin. There are basically two ways
to run a python script in terminal:

Method 1: type the word "python", followed by a space and
the full path to your script, enclosed in quotes, e.g.

python '/Users/dthomaw86/Documents/Python Documents/game_over_enter_key.py'

The quotes are necessary, because you have a space
character in the path.

Method 2:
Step 1: put valid a '#!' line at the top of the script,
for example:

#!/ust/local/bin/python

Step 2: in Terminal, go to the folder where the script is:

cd '/Users/dthomaw86/Documents/Python Documents'

Step 3: make the script "executable"

chmod +x game_over_enter_key.py

Step 4: you can now run the script with:

./game_over_enter_key.py

If you want to run the script again, you only need to repeat
step 4. Or use the "up arrow" key in Terminal.

I think you should pick one method and stick with it, until
you get the hang of Terminal.

> On python.org it states to To run your script from the Terminal
> window you must make sure that /usr/local/bin is in your shell
> search path.

The installer for Mac from python.org has installed python2.7
in /Libary/Frameworks/Python.frameworks/Versions/2.7/bin and
added that directory to you shell search path.

> How can I make sure that the Python I have installed on my Mac
> is in my shell search path.
>
> http://www.freeimagehosting.net/saskk

If you look at the screen shot, you'll see that the shell
has found python in
/Libary/Frameworks/Python.frameworks/Versions/2.7/bin

Incidentally, you don't have to upload screenshots from
Termimal. You can you just select text in Terminal and
use cmd-C to copy it to the clipboard, and then cmd-V to
paste it to this forum. This is a text-only forum, but
text from Terminal _is_ text, so that's allowed.

For example:

HansM 4$ cat game_over_enter_key.py
raw_input("\n\nPress the enter key to exit")
HansM 5$ python game_over_enter_key.py


Press the enter key to exit
HansM 6$

> Thanks again and I am sorry for all the questions,
> I am just getting started on Python

That's all right: most people run into this sort of issues
when they try their first script. There are usually several
ways to do things, and that leads to you getting conflicting
advice.

David Thomas

unread,
Jun 27, 2012, 4:45:47 PM6/27/12
to
Thank you ever so much raw_input works fine. Do you think I should stick with Python 2 before I go to 3?
I have a text book which is using 3 but I've been using an online tutorial which has been helping me lots, which uses version 2.
I found by just typing python then having a space and dragging the file from finder into terminal works.

Hans Mulder

unread,
Jun 27, 2012, 5:33:39 PM6/27/12
to
On 27/06/12 22:45:47, David Thomas wrote:
> Thank you ever so much raw_input works fine.

> Do you think I should stick with Python 2 before I go to 3?

I think so. The differences are not that big, but big
enough to confuse a beginner. Once you know pyhton2,
read http://docs.python.org/py3k/whatsnew/3.0.html
That's the official overview of what has changed between
python2 and python3.

> I have a text book which is using 3 but I've been using an
> online tutorial which has been helping me lots, which uses
> version 2.

If you decide to use the book, you'll want to install Python3
on your Mac. If you do that, you'll find that typing "python"
in Terminal still gets you python2.7; you have to type "pyhton3"
to get python3.

> I found by just typing python then having a space and
> dragging the file from finder into terminal works.

I just tried it, and it works. Terminal uses backslashes
to protect characters that need quoting in the shell. I'd
use single quotes myself, but this also works.

-- HansM

0 new messages