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

import 同名模块有什么好方法?

  •  
  •   datou552211 · Aug 13, 2014 via iPhone · 7386 views
    This topic created in 4291 days ago, the information mentioned may be changed or developed.
    比如我有个叫test.py的脚本,我再它的内部调用import test 但test模块确实存在于site-packages中。在windows中这会默认引用的是test.py==有办法解决嘛
    6 replies    2014-08-13 15:18:06 +08:00
    dreampuf
        1
    dreampuf  
       Aug 13, 2014
    - 尽量避开重名模块
    - 从项目路径导入 from YOURAPP import test
    - 改变 sys.path 顺序
    Zuckonit
        2
    Zuckonit  
       Aug 13, 2014
    名字没取好
    可以加上路径
    pyKun
        3
    pyKun  
       Aug 13, 2014
    以后不要取这种命
    除了一楼的方法还有
    import ..test 类的写法
    timonwong
        4
    timonwong  
       Aug 13, 2014   ❤️ 1
    还有更猥琐的方法
    import imp

    mod = imp.load_source(module_name, fullpath)
    datou552211
        5
    datou552211  
    OP
       Aug 13, 2014 via iPhone
    @dreampuf
    @Zuckonit
    @pyKun
    @timonwong
    绝b不是我的本意,维护代码的时候遇到bug了,然后debug了很久,我了个擦。
    dreampuf
        6
    dreampuf  
       Aug 13, 2014
    @datou552211 print(module.__file__) 事半功倍
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1195 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 23:35 · PVG 07:35 · LAX 16:35 · JFK 19:35
    ♥ Do have faith in what you're doing.