推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
kidlj
V2EX  ›  Python

既然 Python 2 和 Python 3 不兼容,很多 Pypi 包是如何做到既可用于 Python 2,又可用于 Python 3 的?

  •  
  •   kidlj ·
    kidlj · Jan 12, 2015 · 4229 views
    This topic created in 4284 days ago, the information mentioned may be changed or developed.
    7 replies  •  2015-01-12 19:03:56 +08:00
    jiang42
        1
    jiang42  
       Jan 12, 2015   ❤️ 1
    six
    Muninn
        2
    Muninn  
       Jan 12, 2015   ❤️ 1
    关键代码写判断呗。。。

    有的是用six之类的转换,但是不靠谱。。。
    zhicheng
        3
    zhicheng  
       Jan 12, 2015   ❤️ 1
    因为可以 import __future__
    geeklian
        4
    geeklian  
       Jan 12, 2015 via iPhone   ❤️ 1
    也不是完全不兼容,很多库只是名字的区别,甚至就是大小写区别。if version《3:import XXX as xxx else :import xxx
    whtsky
        5
    whtsky  
       Jan 12, 2015   ❤️ 1
    from __future__ import xxx

    try:
    except:

    sys.version_info
    anjunecha
        6
    anjunecha  
       Jan 12, 2015   ❤️ 1
    这是官方关于 __future__ 的文档 https://docs.python.org/2.7/library/__future__.html
    zhyu
        7
    zhyu  
       Jan 12, 2015
    建议看看这个 How to write forwards compatible Python code: http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python/
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2368 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 12:06 · PVG 20:06 · LAX 05:06 · JFK 08:06
    ♥ Do have faith in what you're doing.