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
foxyier
V2EX  ›  Python

大佬们, 请教一个问题: cannot import XXX

  •  
  •   foxyier · 2019-02-01 09:54:25 +08:00 · 2500 次点击
    这是一个创建于 1882 天前的主题,其中的信息可能已经有所发展或是发生改变。

    导入自己写的模块的时候,发生不能导入的情况, 像这样: from send_email import SendMail ImportError: cannot import name 'SendMail'

    用 pycharm 是可以点进去的, 请教排查思路

    8 条回复    2019-02-01 10:35:14 +08:00
    princelai
        1
    princelai  
       2019-02-01 09:58:35 +08:00
    请列出目录结构,PATHONPATH 然后再说
    foxyier
        2
    foxyier  
    OP
       2019-02-01 10:02:40 +08:00
    @princelai 执行文件和 send_email.py 文件是同一级, 我尝试 from src.libs.send_email import SendMail 也不行。
    python35
        3
    python35  
       2019-02-01 10:02:53 +08:00
    export PYTHONPATH=xxx
    arrow8899
        4
    arrow8899  
       2019-02-01 10:04:35 +08:00
    循环引用了
    13817188741
        5
    13817188741  
       2019-02-01 10:06:45 +08:00
    把文件结构列一下看看,另外是不是试试 from .send_email import SendMail
    如果是在同级目录的话.
    princelai
        6
    princelai  
       2019-02-01 10:07:10 +08:00
    首先尝试相对引用
    from .send_email import SendMail

    不行的话在每个目录加上__init__.py 文件再试

    如果还不行那就试试
    sys.path.append(“.”)


    如果还不行我怀疑你安了假 python
    foxyier
        7
    foxyier  
    OP
       2019-02-01 10:12:06 +08:00
    @princelai
    @13817188741
    @arrow8899
    @python35 多谢, 找到原因了。。确实是循环引用了。。还是第一次遇到这个坑。
    Faiz555
        8
    Faiz555  
       2019-02-01 10:35:14 +08:00
    可能是循环引用,可以在具体方法里面引用从而避免
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2902 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 15:18 · PVG 23:18 · LAX 08:18 · JFK 11:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.