How to: Macports select python
Asked Answered
N

5

36

When I enter:

port select --list python

This is the result:

Available versions for python:
    none
    python25 (active)
    python25-apple
    python26-apple
    python27
    python27-apple

I thought when I use python I would be using version 2.5. Instead when I enter "python", version 2.7 seems to be active. How do I change that to version 2.5?

Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Nibelungenlied answered 20/11, 2011 at 13:21 Comment(2)
Are you sure you aren't using the pre-installed python? What does "which python" return?Catharine
Probably it's the preinstalled python: /usr/bin/pythonNibelungenlied
L
32

Why this happens

MacPorts installs binaries into /opt/local by default.

There is also a preinstalled python on your Mac. When just typing python to start, it will start the preinstalled python version not affected by MacPorts install.

To see what version will be executed when just typing python use

which python

To start the mac ports version use

/opt/local/bin/python2.5

Solution

If you wish to always use MacPorts binaries you can change your path so that /opt/local/bin appears before /use/local/bin etc.

/opt/local/bin etc. are added in ~/.tcshrc by MacPorts. Also be sure to look in ~/.profile and ~/.bash_profile as these are default on mac.

Selecting version in ports

First type port select --list python to list installed version, then just for example sudo port select --set python python27 to select 2.7. For more information type port help select.

Legalese answered 20/11, 2011 at 13:31 Comment(11)
thanks, that works, but where is the point in selecting python with macports then?Nibelungenlied
well, in this case there is no real point. Usually there is no program in the /usr/local/bin folder and then it makes sense activating a version in MacPorts.Legalese
The usual macports user adds /opt/local/bin to their path before system directories so gets the python given by port select. You probably have it after.Brisket
Let /opt/local/bin come before /usr/bin. Set the path e.g. this way in .profile or .bash_profile: PATH=/opt/local/bin:$PATH, then either restart Terminal.app or reread the file you changed. An answer on how to pick the desired python is found here: #6117197Ronnieronny
I don't know why the downvote, perhaps someone was taking csh.whynot to heart, and/or mis-understood the question to be about using port select? Anyway, one way you could improve the question is to have more mention of how to change paths in other shells. After all, (t)csh is not the default on macs. So mention of .profile, and/or .bash_profile, etc., would improve your answer.Auxiliaries
@Auxiliaries alright, thanks. I updated the answer. Feel free to edit if I missunderstood something.Legalese
AFAICT macports automatically puts /opt/local/bin before /usr/local/bin in your path on install, so this is unlikely to be the real issue (although may be in the OP's case), the issue is probably best resolved using sudo port select --set python python27 (see easyE's answer for more details)Erring
@user104264 If you actually read the question you will learn that the OP had already selected python 2.5Legalese
@Legalese you're right - I did acknowledge that this "may be the issue in the OP's case" and since he accepted your answer, it's highly likely it was :) My comment was just designed for others, to draw attention to easyE's answer which I think is likely to be the solution that more people coming here are looking for.Erring
To only list and select python3, do port select --list python3 then sudo port select --set python3 <one of the python3's found in the --list command>Ronnieronny
@Erring hm, in my case I use zsh and my ~/.zsh was empty, so I did the export PATH=/opt/local/bin:$PATH myself which fixed it. When exactly would you suppose port updates the zshrc? Would it need quite some parsing?Impala
R
47

Use

osx$ port select --list python

to list your available Python installations.

Then use the "--set" option to "port select" to set the port you wish to use.

osx$ sudo port select --set python python27
Richman answered 5/8, 2013 at 10:33 Comment(1)
Note: This answer doesn't technically answer the original question. It does, however, answer the question that I had which brought me to this page, which was how to select which port was active.Auxiliaries
L
32

Why this happens

MacPorts installs binaries into /opt/local by default.

There is also a preinstalled python on your Mac. When just typing python to start, it will start the preinstalled python version not affected by MacPorts install.

To see what version will be executed when just typing python use

which python

To start the mac ports version use

/opt/local/bin/python2.5

Solution

If you wish to always use MacPorts binaries you can change your path so that /opt/local/bin appears before /use/local/bin etc.

/opt/local/bin etc. are added in ~/.tcshrc by MacPorts. Also be sure to look in ~/.profile and ~/.bash_profile as these are default on mac.

Selecting version in ports

First type port select --list python to list installed version, then just for example sudo port select --set python python27 to select 2.7. For more information type port help select.

Legalese answered 20/11, 2011 at 13:31 Comment(11)
thanks, that works, but where is the point in selecting python with macports then?Nibelungenlied
well, in this case there is no real point. Usually there is no program in the /usr/local/bin folder and then it makes sense activating a version in MacPorts.Legalese
The usual macports user adds /opt/local/bin to their path before system directories so gets the python given by port select. You probably have it after.Brisket
Let /opt/local/bin come before /usr/bin. Set the path e.g. this way in .profile or .bash_profile: PATH=/opt/local/bin:$PATH, then either restart Terminal.app or reread the file you changed. An answer on how to pick the desired python is found here: #6117197Ronnieronny
I don't know why the downvote, perhaps someone was taking csh.whynot to heart, and/or mis-understood the question to be about using port select? Anyway, one way you could improve the question is to have more mention of how to change paths in other shells. After all, (t)csh is not the default on macs. So mention of .profile, and/or .bash_profile, etc., would improve your answer.Auxiliaries
@Auxiliaries alright, thanks. I updated the answer. Feel free to edit if I missunderstood something.Legalese
AFAICT macports automatically puts /opt/local/bin before /usr/local/bin in your path on install, so this is unlikely to be the real issue (although may be in the OP's case), the issue is probably best resolved using sudo port select --set python python27 (see easyE's answer for more details)Erring
@user104264 If you actually read the question you will learn that the OP had already selected python 2.5Legalese
@Legalese you're right - I did acknowledge that this "may be the issue in the OP's case" and since he accepted your answer, it's highly likely it was :) My comment was just designed for others, to draw attention to easyE's answer which I think is likely to be the solution that more people coming here are looking for.Erring
To only list and select python3, do port select --list python3 then sudo port select --set python3 <one of the python3's found in the --list command>Ronnieronny
@Erring hm, in my case I use zsh and my ~/.zsh was empty, so I did the export PATH=/opt/local/bin:$PATH myself which fixed it. When exactly would you suppose port updates the zshrc? Would it need quite some parsing?Impala
S
9

Your shell probably caches the invocation of python and does not look in PATH again. So, when you called python before port select in the same shell session, you need to clear this cache.

For bash, clear the cache using

hash -r

or simply open a new terminal window.

Skinner answered 23/11, 2011 at 11:10 Comment(1)
I found that after invoking the "wrong" version, then using port select, then trying again, it would still invoke the wrong version despite the path to the executable being correct. Thus, this answer might not be the best on it's own, but can be in conjunction with the other answers above.Remediable
C
5

Python installs to:

  • Default (Apple): /usr/local/bin
  • MacPorts: /opt/local/bin
  • python.org: /Library/Frameworks/python...

Default python is required by system so best not to mess with it too much. MacPorts Python convenient to use because getting packages so easy.

You can set link as shortcut:

sudo ln -s /opt/local/bin/python /usr/local/bin/ppython

Then from command-line to use MacPorts version:

ppython script.py
Changeup answered 25/8, 2012 at 11:7 Comment(0)
G
0

An alternative is symlinking each and every Jupyter binary so that the version number does not appear:

cd /opt/local/bin
JUPYTER_VERSION=2.7
for a in jupyter*$JUPYTER_VERSION; do sudo ln -s $a $(echo $a | sed -e 's:-'$JUPYTER_VERSION':g'); done
Galaxy answered 12/7, 2017 at 21:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.