V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
sbmzhcn
V2EX  ›  Python

import parser ,系统已经安装这包 parser,导致 import 不是当前目录下的文件

  •  
  •   sbmzhcn · 2015-01-08 12:42:39 +08:00 · 3097 次点击
    这是一个创建于 3403 天前的主题,其中的信息可能已经有所发展或是发生改变。
    project
    -- parser.py
    -- core.py

    在core.py 使用import parser,然后使用parser中的函数是提示 AttributeError: 'module' object has no attribute

    后来查看原因应该是我电脑中已经安装了包parser导致import时导入的是那个包,不是当前目录下的这个。 请问如何解决,我使用相对路径导入 import .parser仍然不可以。如何在不改名的情况下解决这个问题 ?
    11 条回复    2015-01-10 10:47:10 +08:00
    zhyu
        1
    zhyu  
       2015-01-08 13:46:42 +08:00
    from . import parser
    sbmzhcn
        2
    sbmzhcn  
    OP
       2015-01-08 13:55:46 +08:00
    @zhyu 试过了,不行。
    Sylv
        3
    Sylv  
       2015-01-08 14:04:29 +08:00
    print sys.path 看下,按道理第一项就是 project 目录,所以应该导入的就是 project 下的 parser.py
    hahastudio
        4
    hahastudio  
       2015-01-08 14:09:50 +08:00
    Google: python import name conflict
    不知道 from __future__ import absolute_import 行不行
    https://docs.python.org/2/whatsnew/2.5.html#pep-328-absolute-and-relative-imports
    sbmzhcn
        5
    sbmzhcn  
    OP
       2015-01-08 14:21:02 +08:00
    @Sylv 我也很奇怪。sys.path的第一页的确是当前目录 。
    sbmzhcn
        6
    sbmzhcn  
    OP
       2015-01-08 14:25:12 +08:00
    我这边print parser 提示<module 'parser' (built-in)>
    hiths
        7
    hiths  
       2015-01-08 14:29:36 +08:00
    自己写的那个和自带的包重名了吧。。
    hiths
        8
    hiths  
       2015-01-08 14:30:22 +08:00
    @hiths 擦,没认真审题不好意思。。
    Sylv
        9
    Sylv  
       2015-01-08 15:31:20 +08:00
    >>> print parser
    <module 'parser' from '/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/parser.so'>

    >>> print sys.builtin_module_names
    ('__builtin__', '__main__', '_ast', '_codecs', '_sre', '_symtable', '_warnings', '_weakref', 'errno', 'exceptions', 'gc', 'imp', 'marshal', 'posix', 'pwd', 'signal', 'sys', 'thread', 'xxsubtype', 'zipimport')

    我这边 parser 不在 built-in module 里, 不知道为什么你的 parser 会跑到 built-in 里去了。
    你的 Python 是特别的版本吗?什么平台的?
    sbmzhcn
        10
    sbmzhcn  
    OP
       2015-01-08 16:00:57 +08:00
    @Sylv 一般情况 下不会在里面,我的也不知道为什么在built in里。有可能我装了其它的东西。我是win7 64bit , python 3.3.2


    pip list

    beautifulsoup4 (4.3.2)
    blinker (1.3)
    colorama (0.3.1)
    cssselect (0.9.1)
    cx-Freeze (4.3.2)
    decorator (3.4.0)
    deliciousapi (1.6.7)
    django (1.6.2)
    docutils (0.12)
    ecdsa (0.11)
    feedgenerator (1.7)
    jinja2 (2.7.3)
    lxml (3.3.3)
    Markdown (2.5.1)
    markupsafe (0.23)
    MySQL-Connector-Python (1.1.6)
    networkx (1.9)
    nltk (3.0.0b1)
    numpy (1.9.0b2)
    oauthlib (0.6.3)
    openpyxl (1.8.4)
    paramiko (1.15.1)
    pelican (3.4.0)
    pip (1.5.6)
    pycrypto (2.6)
    pygments (2.0rc1)
    PyMySQL (0.6.2)
    python-dateutil (2.2)
    pytz (2014.9)
    requests (2.2.1)
    requests-oauthlib (0.4.1)
    selenium (2.44.0)
    setuptools (5.4.1)
    six (1.8.0)
    sqlalchemy (0.9.8)
    tld (0.6.3)
    unidecode (0.04.16)
    virtualenv (12.0.5)
    XlsxWriter (0.5.3)
    zhyu
        11
    zhyu  
       2015-01-10 10:47:10 +08:00
    用 virtualenv 开发吧,避免引入不需要的包
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2263 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 09:31 · PVG 17:31 · LAX 02:31 · JFK 05:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.