Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple versions of Python without uninstallation

I am faced with a unique situation, slightly trivial but painful.

I need to use Python 2.6.6 because NLTK is not ported to Python 3 (that's what I could gather).

In a different code(which am working concurrently), there is a collections counter function which is available only in Python 3 but not in Python 2.6.6.

So, each time I switch between the two codes, I need to install & uninstall the versions. That's such a waste of time.

Any suggestions on how I specify which version I want to use?

like image 794
Learnerbeaver Avatar asked May 05 '12 17:05

Learnerbeaver


People also ask

Can I have 2 versions of Python installed?

Switch between different versions You can repeat the above steps and install different versions of python as you want and set priority and use them as needed. To use these python versions with IDE, go to your IDE's interpreter settings and there you can see the different versions of python available on your system.

Can multiple versions of Python run on the same machine?

If you wish to use multiple versions of Python on a single machine, then pyenv is a commonly used tool to install and switch between versions. This is not to be confused with the previously mentioned depreciated pyvenv script. It does not come bundled with Python and must be installed separately.

How do I use another version of Python?

Yes, you should be able to switch between python versions. As a standard, it is recommended to use the python3 command or python3. 7 to select a specific version. The py.exe launcher will automatically select the most recent version of Python you've installed.


2 Answers

Install Python 3

Python 3.3 and higher put a py.exe into the windows folder. [link] This executable is used to determine the python version with the first line of the file:

#!/usr/bin/python2.7

will be executed with Python 2.7. You must install the Python 3 version after you installed the other Python versions.

Additional ressources: https://docs.python.org/3/using/windows.html#customization

pywin https://pypi.python.org/pypi/pywin

Old Solution

I guess you use windows. I solved this problem with a hack:

Every time I start python on windows a python.bat will be used. This starts a python.py that analyses the file for the header after #! for the python version.

To start example.py I type into the console

python example.py

but it could also be started per klick.

this is my python file C:\bin\python.py

#!/usr/bin/env python2
import sys
import os
args = sys.argv
if len(args) <= 1:
    # no arguments
    # start python console
    i = os.system('C:\bin\python2.bat' + " ".join(args[1:]))
    if type(i) != int:
        i = 0
    exit(i)

def analyse(filename, default = ''):
    '''=> '2', '3', default '''
    try:
        f = open(filename)
    except IOError:
        # file not found
        return default
    firstLine = f.readline()
    if firstLine.startswith('#!'):
        if 'python2' in firstLine:
            return '2'
        if 'python3' in firstLine:
            return '3'
        i = firstLine.find(' ')
        if i != -1:
            # analyse from end of path on
            in2 = '2' in firstLine[i:]
            in3 = '3' in firstLine[i:]
            if in2 and not in3:
                return '2'
            if in3 and not in2:
                return '3'
        else:
            # analyse path
            in2 = '2' in firstLine
            in3 = '3' in firstLine
            if in2 and not in3:
                return '2'
            if in3 and not in2:
                return '3'
    return default



no = analyse(args[1], default = '2')
if args[1][-1:] == 'w':
    # python win
    cmd = 'C:\bin\pythonw%s.bat'
else:
    cmd = 'C:\bin\python%s.bat'
i = os.system(cmd % no + ' ' + " ".join(args[1:]))

if type(i) != int:
    i = 0
exit(i)

This is the C:\bin\python.bat file

@echo off
C:\bin\python2 C:\bin\python.py %*
rem this may also work:
rem C:\bin\python.py %*

and in every file you start you have to put either

#!/bin/env/python3

or

#!/bin/env/python2

default is python2

Then I added those files to the folder:

C:\bin\python2.bat

@echo off
C:\python27\python.exe %*

C:\bin\pythonw2.bat

@echo off
C:\python27\pythonw.exe %*

C:\python3.bat

@echo off
C:\python32\pythonw.exe %*

C:\bin\pythonw3.bat

@echo off
C:\python32\pythonw.exe %*

If you are using python26 instead if python27 then you need to change

C:\python27 

to

C:\python26

and so on. Same with python not using python 32.

You may also want to start python files per klick

then do this:

klick right on a .py file -> open with -> select C:\bin\python.bat

If you get problems contact me or leave a comment.

like image 89
User Avatar answered Oct 07 '22 06:10

User


You simply install multiple versions in separate directories, and then you run the python program with the Python version you want to use. Like so:

C:\Python26\Python.exe thescript.py

Or similar.

What virtualenv does is that it gives you many separate "virtual" installations of the same python version. That's a completely different issue, and hence it will not help you in any way.

like image 34
Lennart Regebro Avatar answered Oct 07 '22 06:10

Lennart Regebro