推荐学习书目
› 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 · 4319 views
    This topic created in 4330 days ago, the information mentioned may be changed or developed.
    7 replies  •  2015-01-12 19:03:56 +08:00
    jiang42
    jiang42
    Jan 12, 2015❤️1
    1
    six
    Muninn
    Muninn
    Jan 12, 2015❤️1
    2
    关键代码写判断呗。。。

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

    try:
    except:

    sys.version_info
    anjunecha
    anjunecha
    Jan 12, 2015❤️1
    6
    这是官方关于 __future__ 的文档 https://docs.python.org/2.7/library/__future__.html
    zhyu
    zhyu
    Jan 12, 2015
    7
    建议看看这个 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   ·   2052 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 06:16 · PVG 14:16 · LAX 22:16 · JFK 01:16
    ♥ Do have faith in what you're doing.