1

Is there any danger to adding a new version of python to PATH? i.e. will some important system programs break?

Suppose this is without deleting the old python2, but effectively changing the command "python" to point to python3 (or a non-default version of python2). Is it unsafe even if the change does not affect root?

2
  • If you are replacing python2 with python3, It may be unsafe.
    – ishidex2
    Mar 7, 2019 at 20:47
  • Thanks for the comment, added some more details to the question.
    – Burrito
    Mar 7, 2019 at 20:55

2 Answers 2

1

TLDR ~

You're probably safe but...

There is a danger. This is why the default python on the command line is still python 2 for many distributions.

Will this actually hurt you?

That's impossible to know without knowing which linux distribution you have which packages. I doubt anyone has full oversight of every package available in the world to know what would break if you tried this.

However as time goes on the danger gets less. Python 3 is maturing and those still using python 2 are much more aware of the danger meaning they can protect against it. At this time the danger might be low enough for you to get away with this.

What's the danger?

There are many programs that run on linux which are written in python. You may not have realized you're using python because you invoke them as executable. They use the shebang to invoke python without you explicitly calling it.

The problem was that very many programs were written for python 2, who's shebang was set to #!/usr/bin/python. Likewise shell scripts calling puthon scripts were prone to simply call python script.py. If you changed /usr/bin/python to Python 3 then many programs would break. However as time goes by, most older programs have been modified to request a specific version (/usr/bin/python2 or even /usr/bin/python2.7). Python 3 programs have been forced to specify a version due to the statuesque.

Check before you change

I wouldn't change this on a production server unless you have a really really good reason. However if you're changing your own laptop the risk should be much lower.

I would try running this to be sure:

grep /usr/bin/python /usr/bin/* /usr/sbin/* 2> /dev/null

... look for anything calling /usr/bin/python without specifying a version. If there's nothing there then go ahead and try. Just be on the look out for things breaking unexpectedly.

2
  • Cool, that's basically what I was worried about - programs using python and expecting python 2. That said, if the original /usr/bin/python is left untouched (but PATH points to a new python in another location), then any programs which set the shebang will still call the original python (showing the importance of the shebang ;). The only danger I see then would be if a program calls python expecting python 2, and is running as some user with the new PATH set... which doesn't sound likely...?
    – Burrito
    Mar 8, 2019 at 4:10
  • 1
    okay so leaving /usr/bin/python untouched is much less risky. There is still a small risk from shell scripts. Mar 8, 2019 at 8:17
0

Short of more details as to why you would do what you ask about, it is difficult to write anything but a generic answer.

If you think of adding 3.7.x to your existing 2.7.y, there is in principle no risk of anything bad happening to you. Those two versions of Python will be segregated and carry a distinct dependency tree. They can co-exist on one system without a problem. They can also run simultaneously.

If on the other hand you run an instance of 3.4.z and you want to install 3.7.x in parallel, I would strongly advise against it. Some libraries and dependencies pertaining to different version will no doubt bear the same name and will be replaced by the last install, which would wreck havok in the previous version. You would be better advised to simply upgrade to the latest version of Python in that particular case.

If you NEED to run different library version for different jobs, you can always resort to Jupyter notebooks. They allow you a degree of segregation of your different versions and their packages.

EDIT:

Following your comment, on installing 3.7 and 2.7 side by side, one of your two python version's installation will be on the default path for executables. Say it is python 3.7. The other one will be available as usual by invoking python2 on the cli. If python 2.7 ends up being on your PATH variable, then to run Python 3.7, you will need to issue python3 on the cli. Last to find out which is which, just run python --version in the cli and you will see which can be invoked as python and which must be invoked with a 2 or 3 number suffix.

This is quite usual on most distros and for practically all releases.

2
  • My main concern is, if I add 3.7.x to existing 2.7.y, and the command "python" now points to 3.7.x, is there a chance this will break system processes which need 2.7.y? If not, how are they running 2.7.y when PATH is pointing python to 3.7.x?
    – Burrito
    Mar 7, 2019 at 20:57
  • @Benitok: see my edit.
    – Cbhihe
    Mar 7, 2019 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.