Executing Python Scripts on Mac using Python Launcher

D

David Thomas

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 pieceof 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
 
B

Benjamin Kaplan

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
--

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?
 
D

Dave Angel

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.
 
D

David Thomas

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.



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.



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.



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.

Hi

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.



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.
 
D

David Thomas

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.



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.



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.



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.

Hi

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.



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.
 
D

David Thomas

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

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
 
D

Dave Angel

(You forgot to include the list on this message, but I'm including them
in my reply)

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)
 
B

Benjamin Kaplan

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 syntaxerror.  Sorry I am new to Python and just want to know how I can opensuch 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.
 
H

Hans Mulder

Thank you for the fast response. How can I tell if Python is in the same path as Terminal?

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
 
D

Dennis Lee Bieber


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".

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.

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.


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>
 
H

Hans Mulder

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.
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>


Hope this helps,

-- HansM
 
H

Hans Mulder

That's a typo. You presumably meant:

which python

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
 
D

Dennis Lee Bieber

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

Okay, but...

It may still be cleaner (to non-Mac users) if the execution
preferences still kept the "use terminal" to the python.exe, and left
"pythonw.exe" to "no terminal" applications <G>

Even if the same executable is used in the end...
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.
While I knew what that option meant, I didn't mention it myself as
the OP probably (based on the nature of the questions) wouldn't
understand the meaning... (And, on Windows at least, #! lines don't do
anything <G>)
 
D

Dave Angel

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.
 
D

David Thomas

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

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
 
H

Hans Mulder

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.


Hope this helps,

-- HansM
 
D

David Thomas

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top