PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 14, 2022

[FIXED] How to make an import statement work on both python 2 and python 3

 April 14, 2022     migration, python     No comments   

Issue

I'm doing a migration for a code from python 2 to python 3. there is some code that I'm not migrating and is necessary to the code that i do migrating, therefor I need that some of the import statements work on both versions but the package got its name changed for example:

import urlparse  # Python2
import urllib.parse as urlparse  # Python 3

how can i code on statement that will work on both versions. keep in mind that this question is for the general case (the example above is only one of the problems created by the following migration)


Solution

For your imports, you can do the following:

import sys
if sys.version_info[0] < 3:
    #import your py2 packages
else:
    #import your py3 packages


Answered By - acrobat
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing