推荐学习书目
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
RIcter
V2EX  ›  Python

如果两个 Python 的第三库名字相同,如何去区分?

  •  
  •   RIcter ·
    RicterZ · Mar 24, 2015 · 4229 views
    This topic created in 4098 days ago, the information mentioned may be changed or developed.

    python 中有一个 websocket 和 websocket-client,用法都是 import websocket
    现在有一个需求是判断 websocket-client 是否安装,有没有 identity 可以判断到底是 websocket-client 还是 websocket?

    分分分分分分分分分分分割线线线线线线线线线线线线

    另外,有个这样的需求..

    try:
        xxx
    except (websocket.WebSocketException, sock.error, urllib.xxxx) as e:
        xxx
    

    当 websocket 库不存在的时候如何保证不报错,当安装的时候可以 websocket.WebSocketException 这个错误?最好的保持上述结构,因为结构实在是很难变动的...(好几百行orz)

    18 replies    2015-03-26 18:41:12 +08:00
    mikangchan
        1
    mikangchan  
       Mar 24, 2015
    前排跪舔/w\
    geeklian
        2
    geeklian  
       Mar 24, 2015
    两个库名字一样=.=
    不是子集的关系,那就是违规了吧?
    est
        3
    est  
       Mar 24, 2015   ❤️ 1
    try:
    import websocket
    websocket.特别的method
    except:
    exit('木有安装')
    RIcter
        4
    RIcter  
    OP
       Mar 24, 2015
    @est thx,这个我倒是想过,只是想问问有没有别的更好的方法喵QAQ..
    tidewind
        5
    tidewind  
       Mar 24, 2015
    print websocket.__name__试试呢
    RIcter
        6
    RIcter  
    OP
       Mar 24, 2015
    @tidewind 这俩都一样..
    Septembers
        7
    Septembers  
       Mar 24, 2015   ❤️ 1
    print(map(lambda x: getattr(x, 'project_name'), pkg_resources.require('websocket')))

    see https://stackoverflow.com/questions/2058802
    scys
        8
    scys  
       Mar 25, 2015
    将库下载下来,直接修改名字,然后再import~
    Septembers
        9
    Septembers  
       Mar 25, 2015
    @scys 客户环境/生产环境 不是想动就能动的
    22too
        10
    22too  
       Mar 25, 2015
    难道不能as 吗??
    import websocket as web
    RIcter
        11
    RIcter  
    OP
       Mar 25, 2015 via iPad
    @22too
    乃没看懂我的需求…
    我是判断 import 的库是不是我想 import 的那个
    yegle
        12
    yegle  
       Mar 25, 2015
    判断 websocket-client 是否安装

    这个活读取pip freeze的输出不行吗?或者

    from pip install get_installed_distributions
    RickGray
        13
    RickGray  
       Mar 25, 2015 via iPhone
    查看 __doc__ :)
    22too
        14
    22too  
       Mar 25, 2015
    @RIcter 明白了!
    Melodic
        15
    Melodic  
       Mar 25, 2015
    楼主看上去好面善
    RIcter
        16
    RIcter  
    OP
       Mar 25, 2015
    @Melodic 这都能被看粗来!
    Madimo
        17
    Madimo  
       Mar 26, 2015 via iPhone
    难道不是自己写个工厂类再封装一层就可以了么...
    RIcter
        18
    RIcter  
    OP
       Mar 26, 2015
    @Madimo _(:з」∠)_啥
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3223 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 88ms · UTC 03:18 · PVG 11:18 · LAX 20:18 · JFK 23:18
    ♥ Do have faith in what you're doing.