One of the things I found difficult when first using Python on a Mac was simply getting my scripts to run as programmes, with a simple double-click. The nub of the problem is that one must run a script using the python interpreter, like so:
python script.py
This is easy enough to do in a command window, terminal window, at the shell prompt etc. (The terminology is different depending on the operating system, but the reality is the same.)
On Windows I would write a one-line batch file containing the command above. This could be then be treated in all ways like a self-contained application. Simple.
I had difficulty making anything like this work on Mac OS X until I did the following three things:
1. Make the script executable:
chmod a+x
2. Ensure the first line in the script is a "shebang line", telling it where to find the interpreter:
#!/usr/bin/env python
Er, hold on, that looks correct for LINUX but not the Mac. Anyway, be sure to put in the proper path!
3. Associate the script file with PythonLauncher. One does this by right-clicking (option-click) the script, selecting "open with", and then finding this programme.
In my case I had difficulties because the first PythonLauncher to be available in the menu was actually for an older Python version. It is quite common on the Mac and LINUX for there to be multiple versions of Python. This is because the operating systems come with one installed, and it is often best to leave that in situ so that any system tools that need it can still use it properly. But one inevitably wants to upgrade to the latest greatest version. The solution is to install this in parallel.
I had my script running with Python 2.3 when it needed 2.6. I got no error or indeed any feedback that something was wrong. This took me a while to figure out!
I had a second big problem, but that will wait for my next article.
I had difficulty making anything like this work on Mac OS X until I did the following three things:
1. Make the script executable:
chmod a+x
2. Ensure the first line in the script is a "shebang line", telling it where to find the interpreter:
#!/usr/bin/env python
Er, hold on, that looks correct for LINUX but not the Mac. Anyway, be sure to put in the proper path!
3. Associate the script file with PythonLauncher. One does this by right-clicking (option-click) the script, selecting "open with", and then finding this programme.
In my case I had difficulties because the first PythonLauncher to be available in the menu was actually for an older Python version. It is quite common on the Mac and LINUX for there to be multiple versions of Python. This is because the operating systems come with one installed, and it is often best to leave that in situ so that any system tools that need it can still use it properly. But one inevitably wants to upgrade to the latest greatest version. The solution is to install this in parallel.
I had my script running with Python 2.3 when it needed 2.6. I got no error or indeed any feedback that something was wrong. This took me a while to figure out!
I had a second big problem, but that will wait for my next article.
RELATED POSTS
2 comments:
Actually you'll find that using /usr/bin/env on OS X for the shebang line is fine, OS X comes with /usr/bin/env for getting the environment dependant path to executables, e.g. /usr/bin/env python will yield the python path for the local env (which can be changed, e.g. by using virtualenv).
Good practice for scripts to run correctly under most POSIX environments.
Perhaps not good practice, but I find '#!python' to be just as effective and a lot easier to remember. YMMV.
Post a Comment