0

my OS (Ubuntu 14.04) uses an older version 2.7.6 of Python as /usr/bin/python which points to /usr/bin/python2.7 I installed a new version 2.7.9 of python as /usr/local/bin/python2.7.

In my own shell,

  • how can I make all python scripts with shebang to use the new python? Does use #!/usr/bin/env python as shebang solve the problem? How shall I make it work?

  • Can I also make the command python be the new one?

while letting my OS still use its original older one?

2
  • not yet., @Mark
    – Tim
    Mar 17, 2015 at 14:15
  • 1
    Ah, your updated question makes things easier. Many people add /usr/local/bin to the beginning of their search path so that they can run newer versions of system programs. If this is OK with you, run sudo ln -s /usr/local/bin/python2.7 /usr/local/bin/python, then edit your own ~/.bash_profile to include a line that says PATH=/usr/local/bin:$PATH. Logout, login again. Run python --version to verify. Mar 17, 2015 at 14:19

3 Answers 3

4

To set things up so that python gets you the new version but everyone else, including the standard OS programs, will get the original:

  • choose a directory to hold your personal programs (or symlinks to them). This could be $HOME/bin or /usr/local/bin, whatever you like. Create it if it doesn't exist. I'll use $HOME/bin in this example.

  • Edit your ~/.profile. On Ubuntu 14.04, it likely already contains a line that says PATH="$HOME/bin:$PATH", which will put this directory at the front of your search path. If not, add that line.

  • run ln -s /usr/local/bin/python2.7 $HOME/bin/python

  • logout, login, type which python to verify that the shell is finding the (symlink) python in your $HOME/bin directory. Type python --version to verify that you're getting the new version.

You're already doing a good thing by having your scripts start with the line #!/usr/bin/env python rather than #!/usr/bin/python. The env command will look through your search path to find python.

5
  • with #!/usr/bin/env python, what do I do outside the script to use the new python automatically? Just add the path of the new python to my PATH?
    – Tim
    Mar 17, 2015 at 14:47
  • You add the directory that contains the new python (or contains a symlink to the new python) to the front of PATH. PATH contains just directories, each separated by a colon. Mar 17, 2015 at 14:49
  • will my OS still use its original and old python?
    – Tim
    Mar 17, 2015 at 14:49
  • +1..good use of $HOME/bin
    – heemayl
    Mar 17, 2015 at 14:50
  • @Tim It should. If you do cd /usr/bin; grep python *, you can see that every system program there that needs python uses the full pathname: #!/usr/bin/python, #!/usr/bin/python3, etc. Operating system vendors fully expect people will have their own private versions of things like python coexisting with the system versions. Mar 17, 2015 at 14:52
1

how can I make all python scripts with shebang to use the new python?

Use the shebang #!/usr/bin/python2.7

Can I also make the command python be the new one?

You can name the compiled Python something else e.g. /usr/local/bin/python2.7 and then directly call the binary by full path or create an alias like

alias python2.7='/usr/local/bin/python2.7'

and put it in ~/.bashrc.

5
  • Thanks. (1) sorry I messed up the directory for my new python. see my edit. (2) I heard that Ubuntu 14.04 must use its default python, and can't use the new python I installed. So if I make /usr/bin/python point to my new python, will that make my OS not work properly?
    – Tim
    Mar 17, 2015 at 14:17
  • @Tim: Answer edited..hope its clear now
    – heemayl
    Mar 17, 2015 at 14:21
  • Whether it is allowed to replace the default python with newer python, see here askubuntu.com/questions/103772/…, and askubuntu.com/questions/597855/…. From the first link, I understand that it is not allowe, and Ubuntu needs the default old python.
    – Tim
    Mar 17, 2015 at 14:29
  • @Tim: Now i get whats your real query is..yes, its true that many python modules will be unavailable and also the programs depending on those modules won't simply work..your best bet would be to name it something else like /usr/local/bin/python2.9 and then directly call the binary by full path or create an alias like alias python2.9=/usr/local/bin/python2.9
    – heemayl
    Mar 17, 2015 at 14:36
  • @Tim: you can use alias or use as other answer pointed out to symlinked to $HOME/bin ..it will make sure no system program will use this one..normally those programs use full path to the binary, if some program is not using it then they will search through the PATH which will not include your $HOME/bin..
    – heemayl
    Mar 17, 2015 at 14:49
0

For this purpose you may also use Virtualenv, it allows you to use different python versions inside.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .