V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
Feiox
V2EX  ›  Python

大家有没有被 PyCharm 的“想对引用”坑过

  •  
  •   Feiox ·
    feiox · Dec 16, 2014 · 9641 views
    This topic created in 4156 days ago, the information mentioned may be changed or developed.
    只要一使用,就报错 ValueError: Attempted relative import in non-package

    即使使用

    ```
    import os
    os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
    ```

    也无解。求指导 ~
    4 replies    2014-12-19 20:31:55 +08:00
    polythene
        1
    polythene  
       Dec 16, 2014
    1、是“相对引用”。
    2、relative import跟PyCharm没有关系,这是python的语法规定,在非包环境中就不能用相对引用,只能用绝对引用。
    staticor
        2
    staticor  
       Dec 16, 2014
    可参考 python cookbook, 应该是chapter 10 Modules and Packages 讲了一些包管理 相对引用的东西.
    如上, 题主的问问题的问法有失偏颇.
    Feiox
        3
    Feiox  
    OP
       Dec 18, 2014
    @polythene
    @staticor
    因为我用相对引用时, 使用命令行可以运行,使用 pycharm 就会报错。百思不得解 ~
    laike9m
        4
    laike9m  
       Dec 19, 2014
    @Feiox 这个是你的执行路径不同,pycharm 里可以设定 working directory
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2655 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 03:43 · PVG 11:43 · LAX 20:43 · JFK 23:43
    ♥ Do have faith in what you're doing.