1

Using Ubuntu 16.04.

I was doing development in Python2.7, although recently moved to Python3.5, both of which come by default. The problem is, I find all the python libraries have to be reinstalled or downloaded for the next Python3. Also, the behaviour of Python seems weird because to execute the same script in python3, I have to enter in terminal

python3 script.py

which is different from other applications where I do not have to give the version number. Anyway, the questions I am trying to find answers are

  1. To what extent are the libraries, packages (such as pip etc.) shared between 2.7 and 3.5? Or do I need double installations (and double the space) for everything now? A bit space limited in my old laptop.
  2. Most of the installation instructions and commands I find online do not specify whether they are for 2 or 3. Given that I have two versions, how do I control/make sure they go to 3.5?
  3. Is it advisable and possible to completely remove everything related to 2.7? Can I keep working with 3.5?
bhansa
  • 7,282
  • 3
  • 30
  • 55
Della
  • 1,264
  • 2
  • 15
  • 32
  • 1
    you should use virtualenv – karansthr Sep 28 '17 at 10:17
  • You can keep working with python3. No need to uninstall python-2.7 . For installation of packages with pip , use the following commands: python -m pip install for python-2.7 and python3 -m pip install for python3+. – Bhawan Sep 28 '17 at 10:18
  • http://docs.python-guide.org/en/latest/dev/virtualenvs/ Tried this. python --version gives 2.7.12. python3 --version gives 3.5.2. Which one do I 'really' have? – Della Sep 28 '17 at 10:20
  • You have both the versions installed, python2 links to python directory and python3 is linked to the other directory. Use virtual env to keep using both. – bhansa Sep 28 '17 at 10:23
  • If you have both python 2 and 3 installed on your system. `python` will usually refer to python2 and `python3` obviously refers to python3. Nothing weird about it. To answer your point number 2: they either have only 1 version of python installed or they are running the command with python2. – Arash Rohani Sep 28 '17 at 11:36

2 Answers2

2

The first thing you need to know is that all official libraries and python tools for python3 got the "3" character to separate them from the previous versions. So, you need to use pip3, and not pip, python3, not python, and the packages are called python-pygame, not python-pygame. So, to answer to you in order:

  1. Yes, the you have to double the space needed if you decide to use both python2 and python3
  2. Usually, if in the tool name there is 3, it is for python3, and if not it's for python2
  3. Python2 and python3 are completly indipendent (different path, indipendent versions, etc.) so having python 2.7 installed doesn't affect python3 BUT since python 3 is the next version of python 2, it makes it obsolete (in my opinion) so if you don't have enough space for both, keeping python2 is absolutely not needed nor useful
jthulhu
  • 7,223
  • 2
  • 16
  • 33
  • Thanks. Have one more tangential question. The way python2.7 and python3.5 are independent, is python3.4 independent of these too, if I choose to have it? Some libraries seem to be compatible with 3.4, but not 3.5. – Della Oct 02 '17 at 02:20
  • @Della I'm writing this answer quickly before checking and editing my answer, so take it carefully: no, I don't think python3.4 is indipendent from python3.5 since I usually only specify the version, and not the subversion (eg. I use python3, pip3 but not python3.4 or 3.5 or whatever). The best tip I can give you about this is letting the linux packaging do it for you if you can: do # apt-get install python3 to install python3, # apt-get install python3-_packagename_, for libraries – jthulhu Oct 03 '17 at 16:10
1

If you want to control your python execution then you have various method or techniques:

For downloading packages according to version

You can use pip{version-name} to download the libraries. Like if you want to download library of python 2.7 then write

pip2.7 install package-name

for python 3.5 then use

pip3.5 install package-name

For execution of program:

If you want to execute the program accoridng to you choice of version then just use

python{version-name} script.py

eg:

python2 script.py

python3 script.py

or you just write down the path of your python version on the top of script. Please refer this for more details: Why do people write #!/usr/bin/env python on the first line of a Python script?

P.Madhukar
  • 454
  • 3
  • 12