Muddleglum Smith
Muddleglum Smith

Reputation: 11

python tkinter import portability

I'm running the same very small python script from both my work computer at home and a server at work that that computer RDPs to. The server uses the company standard python 2 and I have been using python 3 at home. Recently I decided to use the same script to do the same job on both. Everything is the same and works except that one wants:

from Tkinter import *

and the other wants a single letter case change:

from tkinter import *

How do I make this portable as in the same script working on different python environments? I don't want to have two scripts to remember to keep an eye on. Is it possible?

Upvotes: 0

Views: 185

Answers (3)

aliva
aliva

Reputation: 5720

or try importing both of them :-)

try:
    from tkinter import *
except ImportError:
    from Tkinter import *

Upvotes: 0

Parviz Karimli
Parviz Karimli

Reputation: 1287

Check the Python version and treat according to the case:

import sys

if sys.version_info[0] > 2:
    from tkinter import *
else:
    from Tkinter import *

Upvotes: 1

holdenweb
holdenweb

Reputation: 37033

The 2to3 program will perform (some, but not always perfect) translation of your program to Python 3. The trick is to write the code in such a way that your Python 2 does get successfully (and correctly) converted.

I'd recommend you take a look at python-modernize, which should help you maintain a compatible source.

Upvotes: 1

Related Questions