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

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

  •  
  •   Feiox ·
    feiox · 2014-12-16 22:08:03 +08:00 · 9117 次点击
    这是一个创建于 3390 天前的主题,其中的信息可能已经有所发展或是发生改变。
    只要一使用,就报错 ValueError: Attempted relative import in non-package

    即使使用

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

    也无解。求指导 ~
    4 条回复    2014-12-19 20:31:55 +08:00
    polythene
        1
    polythene  
       2014-12-16 22:27:45 +08:00
    1、是“相对引用”。
    2、relative import跟PyCharm没有关系,这是python的语法规定,在非包环境中就不能用相对引用,只能用绝对引用。
    staticor
        2
    staticor  
       2014-12-16 23:03:58 +08:00
    可参考 python cookbook, 应该是chapter 10 Modules and Packages 讲了一些包管理 相对引用的东西.
    如上, 题主的问问题的问法有失偏颇.
    Feiox
        3
    Feiox  
    OP
       2014-12-18 13:39:10 +08:00
    @polythene
    @staticor
    因为我用相对引用时, 使用命令行可以运行,使用 pycharm 就会报错。百思不得解 ~
    laike9m
        4
    laike9m  
       2014-12-19 20:31:55 +08:00
    @Feiox 这个是你的执行路径不同,pycharm 里可以设定 working directory
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5408 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:38 · PVG 16:38 · LAX 01:38 · JFK 04:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.