BBEdit & Python

1,280 views
Skip to first unread message

Chip G.

unread,
Feb 11, 2013, 3:25:17 PM2/11/13
to bbe...@googlegroups.com
I'm trying to learn Python. I have a book that is helping me along. I'd like to use BBEdit as my editor, but I'm having some issues. Part of this is an Apple/Fink thing. Part of the problem is that the version of python get if I run using 'python' is old (v2.7.1). If I force paths I can get v2.7.3 or v3.2.3. As near as I can tell, BBEdit uses the version as if I typed 'python' which doesn't work properly with the code in the book. Even if I download the authors copy directly. So I'm trying to find a way to force BBEdit to use a specific version 2.7.3 or 3.2.3 vice the 2.7.1 it keeps preferring. Any ideas?


--
Chip

Maarten Sneep

unread,
Feb 11, 2013, 4:12:35 PM2/11/13
to bbe...@googlegroups.com

On 11 feb. 2013, at 21:25, "Chip G." <n1mie...@gmail.com> wrote:

> I'm trying to learn Python. I have a book that is helping me along. I'd like to use BBEdit as my editor, but I'm having some issues. Part of this is an Apple/Fink thing. Part of the problem is that the version of python get if I run using 'python' is old (v2.7.1). If I force paths I can get v2.7.3 or v3.2.3. As near as I can tell, BBEdit uses the version as if I typed 'python' which doesn't work properly with the code in the book. Even if I download the authors copy directly. So I'm trying to find a way to force BBEdit to use a specific version 2.7.3 or 3.2.3 vice the 2.7.1 it keeps preferring. Any ideas?

What's the first line of your script?

If python 2.7.1 rejects the code, then I assume that the code is written for python 3 (which is a sensible version to learn these days).

You can try to set the first line to:

#!/usr/bin/env python3

If that doesn't work, try to be more specific (open a terminal, and see which versions of python are available, typing python and pressing the tab-key should provide some options).

As a last resort, you could use the full path to python:

#!/usr/local/bin/python3

I'm sure others will provide the correct method for getting BBEdit to recognize your scripts and finding the appropriate interpreter.

Maarten

Chip G.

unread,
Feb 11, 2013, 5:13:06 PM2/11/13
to bbe...@googlegroups.com
On Feb 11, 2013, at 16:12, Maarten Sneep wrote:

> On 11 feb. 2013, at 21:25, "Chip G." <n1mie...@gmail.com> wrote:
>
>> I'm trying to learn Python. I have a book that is helping me along. I'd like to use BBEdit as my editor, but I'm having some issues. Part of this is an Apple/Fink thing. Part of the problem is that the version of python get if I run using 'python' is old (v2.7.1). If I force paths I can get v2.7.3 or v3.2.3. As near as I can tell, BBEdit uses the version as if I typed 'python' which doesn't work properly with the code in the book. Even if I download the authors copy directly. So I'm trying to find a way to force BBEdit to use a specific version 2.7.3 or 3.2.3 vice the 2.7.1 it keeps preferring. Any ideas?
>
> What's the first line of your script?

It starts with a line with a '#' and the name of the file. Next after that is:

from tkinter import *

Which works under python 3.2 but not in python 2.7 unless I make it Tkinter.

If I do
$python myscript.py
It uses 2.7.1.

If I do
$python2.7 myscript.py
It uses 2.7.3.

If I do
$python3.2 myscript.py
It uses 3.2.3.

If I run it from BBEdit it runs behaves the same as the first example. My script, which is calling for Tk to draw a very basic window, doesn't work under 2.7.1 but does under 2.7.3 and 3.2.3. I can test this from the command line perfectly. What I need is a way to control what BBEdit is using.

> If python 2.7.1 rejects the code, then I assume that the code is written for python 3 (which is a sensible version to learn these days).

Nope, works under 2.7.3. And if your statement is true it still doesn't explain how to force BBEdit to use 3.

> You can try to set the first line to:
>
> #!/usr/bin/env python3

That looks like bash script, will that work at the beginning of a python script?

Tried it and BBEdit gave me this error:
"env: python3: No such file or directory"

> If that doesn't work, try to be more specific (open a terminal, and see which versions of python are available, typing python and pressing the tab-key should provide some options).

See above.

> As a last resort, you could use the full path to python:
>
> #!/usr/local/bin/python3

No, still getting the same errors (tried forcing 2.7.3 too, no dice).

> I'm sure others will provide the correct method for getting BBEdit to recognize your scripts and finding the appropriate interpreter.

I hope so ... :)


--
Chip

Lee Hinde

unread,
Feb 11, 2013, 5:37:17 PM2/11/13
to bbe...@googlegroups.com
try this in terminal:

which python3

then use the resulting path as above.


Maarten Sneep

unread,
Feb 11, 2013, 5:42:41 PM2/11/13
to bbe...@googlegroups.com

On 11 feb. 2013, at 23:13, "Chip G." <n1mie...@gmail.com> wrote:

> On Feb 11, 2013, at 16:12, Maarten Sneep wrote:
>
>> On 11 feb. 2013, at 21:25, "Chip G." <n1mie...@gmail.com> wrote:
>>
>>> I'm trying to learn Python. I have a book that is helping me along. I'd like to use BBEdit as my editor, but I'm having some issues. Part of this is an Apple/Fink thing. Part of the problem is that the version of python get if I run using 'python' is old (v2.7.1). If I force paths I can get v2.7.3 or v3.2.3. As near as I can tell, BBEdit uses the version as if I typed 'python' which doesn't work properly with the code in the book. Even if I download the authors copy directly. So I'm trying to find a way to force BBEdit to use a specific version 2.7.3 or 3.2.3 vice the 2.7.1 it keeps preferring. Any ideas?
>>
>> What's the first line of your script?
>
> It starts with a line with a '#' and the name of the file. Next after that is:

That is just a plain comment.

> from tkinter import *
>
> Which works under python 3.2 but not in python 2.7 unless I make it Tkinter.

As expected.

> If I do
> $python myscript.py
> It uses 2.7.1.
>
> If I do
> $python2.7 myscript.py
> It uses 2.7.3.
>
> If I do
> $python3.2 myscript.py
> It uses 3.2.3.

So you have three python interpreters in your path. Don't worry, I think I have 5 or so.

> If I run it from BBEdit it runs behaves the same as the first example. My script, which is calling for Tk to draw a very basic window, doesn't work under 2.7.1 but does under 2.7.3 and 3.2.3. I can test this from the command line perfectly. What I need is a way to control what BBEdit is using.
>
>> If python 2.7.1 rejects the code, then I assume that the code is written for python 3 (which is a sensible version to learn these days).
>
> Nope, works under 2.7.3. And if your statement is true it still doesn't explain how to force BBEdit to use 3.

It does, read again. Right now there is nothing in the file that tells BBEdit which python to run, so it uses the plain default ("python", which resolves to 2.7.1 on your machine).

>
>> You can try to set the first line to:
>>
>> #!/usr/bin/env python3
>
> That looks like bash script, will that work at the beginning of a python script?

That magic #! combination tells the shell to look inside the file to find the correct interpreter. It will run /usr/bin/env with the argument 'python3'. This is a fancy way of saying that you want your environment to figure out which interpreter to run. Works with bash, python, perl, ...

See page 314 in the BBEdit user manual.

> Tried it and BBEdit gave me this error:
> "env: python3: No such file or directory"

So it is working. No, really: BBEdit ran the env tool, but the env tool did not find python3. You could try python2.7 instead, or python3.2, but I'd rather get python3 working correctly.

What does "which python3" in the terminal say? The python3 command is a generic description of, well, python3, without tying you directly to python 3.2.3 or even 3.2.x

>> If that doesn't work, try to be more specific (open a terminal, and see which versions of python are available, typing python and pressing the tab-key should provide some options).
>
> See above.

And now with the full paths.

>
>> As a last resort, you could use the full path to python:
>>
>> #!/usr/local/bin/python3
>
> No, still getting the same errors (tried forcing 2.7.3 too, no dice).

(did you replace the hard-coded path with the output from "which python3.2"?)

Maarten

Chip G.

unread,
Feb 11, 2013, 5:31:25 PM2/11/13
to bbe...@googlegroups.com
On Feb 11, 2013, at 16:12, Maarten Sneep wrote:

> As a last resort, you could use the full path to python:
>
> #!/usr/local/bin/python3

OK, a variation on this worked. When I changed it to a legitimate path on my system it worked.

#!/sw/bin/python3.2

Now that's working. But since the examples I had from the author didn't include those, I wonder why I can't get it to work without.

Thanks again,


--
Chip

Herbert Schulz

unread,
Feb 11, 2013, 6:36:00 PM2/11/13
to bbe...@googlegroups.com
Howdy,

If I had to guess I would assume /sw/bin was placed near the start of the PATH so /sw/bin/python (which most likely is a symbolic link to python3.2) was found before the other versions of python in other items along the PATH.

Good Luck,

Herb Schulz
(herbs at wideopenwest dot com)



Bruce Van Allen

unread,
Feb 11, 2013, 7:03:29 PM2/11/13
to bbe...@googlegroups.com
On 2013-02-11 at 2:31 PM, n1mie...@gmail.com (Chip G.) wrote:

>OK, a variation on this worked. When I changed it to a
>legitimate path on my system it worked.
>
>#!/sw/bin/python3.2
>
>Now that's working. But since the examples I had from the
>author didn't include those, I wonder why I can't get it to
>work without.

I'm not sure what you mean by working "without", but there's a
key thing here that I hope you get.

That first line starting the #! should have a valid path to the
interpeter you want to execute the file with. It's not the path
of the script file itself, which I think you said in an earlier post.

If this isn't familiar, search Wikipedia or somewhere for
"shebang line".

Study what Maaerten said; and I concur with Herbert's guess
about why your script worked with 'sw/bin/python3.2' as the
shebang path. Lee gave you the way to find the paths to ALL of
your Python installs.

If you wanted, by the way, to test your script in different
versions of Python, you can stack up several shebang lines, and
change which one is on the top line to see how the script
performs with your different versions. You could even use
BBEdit's "Move Line Up" command to quickly make the change.

HTH.




Best Regards,

- Bruce

_bruce__van_allen__santa_cruz_ca_

Chip G.

unread,
Feb 18, 2013, 8:19:12 PM2/18/13
to bbe...@googlegroups.com
There was no result, just blankness.

On Feb 11, 2013, at 17:37, Lee Hinde wrote:

> try this in terminal:
>
> which python3
>
> then use the resulting path as above.



--
Chip

Chip G.

unread,
Feb 18, 2013, 8:29:43 PM2/18/13
to bbe...@googlegroups.com
This is a great little gem. I have fiddled with unix for awhile, but there are still holes in my knowledge. This showed me a new scenario where I had multiple versions of an important system component and had to use direct paths to access it. This will take some getting used to.

Thanks for the help all.

On Feb 11, 2013, at 19:03, Bruce Van Allen wrote:

> If you wanted, by the way, to test your script in different versions of Python, you can stack up several shebang lines, and change which one is on the top line to see how the script performs with your different versions. You could even use BBEdit's "Move Line Up" command to quickly make the change.



--
Chip

Chip G.

unread,
Feb 18, 2013, 8:22:42 PM2/18/13
to bbe...@googlegroups.com
Which is pretty much what I managed to determine. The path I get with "which python3.2" is /sw/bin/python3.2 and I put that after a #! and it works.

But what's the best way to get which python3 to work and not break when the software is upgraded?

On Feb 11, 2013, at 17:42, Maarten Sneep wrote:

> So it is working. No, really: BBEdit ran the env tool, but the env tool did not find python3. You could try python2.7 instead, or python3.2, but I'd rather get python3 working correctly.
>
> What does "which python3" in the terminal say? The python3 command is a generic description of, well, python3, without tying you directly to python 3.2.3 or even 3.2.x



--
Chip

Maarten Sneep

unread,
Feb 19, 2013, 3:43:06 PM2/19/13
to bbe...@googlegroups.com

On 19 feb. 2013, at 02:22, Chip G. <n1mie...@gmail.com> wrote:

> Which is pretty much what I managed to determine. The path I get with "which python3.2" is /sw/bin/python3.2 and I put that after a #! and it works.
>
> But what's the best way to get which python3 to work and not break when the software is upgraded?

If which python3.2 returns a result, then a link should be available that points python3 to python3.2. If it isn't, then I would say that whatever system you used to install python is broken (/sw/bin looks a lot like Fink). Complain to Fink.

The /usr/bin/env program was invented just for this reason. It will search the $PATH for a binary of the given name, start it, and pass the script to it for execution.

Try:

#!/usr/bin/env python3.2

as the first line of your script. Now if the installers behaved properly, then python3 would exist, and point to the actual binary. That would then be your upgrade path.

Best,

Maarten
Reply all
Reply to author
Forward
0 new messages