推荐学习书目
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 · 4015 views
    This topic created in 4175 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   ·   Solana   ·   2546 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 07:32 · PVG 15:32 · LAX 00:32 · JFK 03:32
    ♥ Do have faith in what you're doing.