执行:
python deom/scripts/populate.py

ValueError: Attempted relative import in non-package

solve:python import时采用了相对路径,使用-m运行

执行:

python -m demo.scripts.populate


参考:http://my.oschina.net/leopardsaga/blog/97175

1. 从查到的资料来看,关于import路径的来说,分成3类:
  • absolute import
import xml
import youpackage.xml
from youpackage import xml
这几种都算绝对路径
 
  • relative import
import xml
从这个语句上是看不出来import的是标准库的xml,还是你的包里的一个库叫xml。
 
  • explicit relative import
from . import xml
from .xml import some_thing
from ..xml import some_thing
这些以点开头的import明确的表示了import的是相对路径,从而避免了普通relative import的麻烦。
 
2. 第二种(relative import)肯定是不推荐的。
  • 在python2.5以前,如果当前包中有一个叫xml的库,局部的这个xml就会shadow标准库的xml(和局部变量感觉类似,我犯过好几次这个错误,调试的时候还一定能反应过来)。
  • 在2.5和2.6,这个行为还是一样的,但是如果有以下这个语句,局部的包将不能覆盖全局的包
from __future__ import absolute_import
  • 在2.7中,absolute_import的行为变成了默认行为,如果需要应用局部的包,那就得用明确的相对引用了。
文档是这么描述的,但通过-m运行和直接运行py的行为还是会不一样,看下面这个例子:
$ python --version
Python 2.6.5
 
$ cat bar/baz.py 
from __future__ import absolute_import
import xml
print xml.__file__
 
$ python bar/baz.py 
/home/huanghao/workspace/test/bar/xml.pyc
 
$ python -m bar.baz
/usr/lib/python2.6/xml/__init__.pyc
 
看上去很奇怪,通过py运行的时候absolute_import好像根本没有作用,原因在这个blog里有解释
 
3.所以说relative import麻烦事太多,一定不能用了。但是absolute和explicit relative这两个到底用哪个?
PEP8中提到:
Relative imports for intra-package imports are highly discouraged. Always use the absolute package path for all imports. Even now that  PEP 328  is fully implemented in Python 2.5, its style of explicit relative imports is actively discouraged; absolute imports are more portable and usually more readable.
 
PEP8还是建议用absolute,即使PEP328实现了explicit relative。
本来我觉得到此就为止了。 但是今天还是有同事非要用explicit relative import,跟我说因为absolute写起来太长,而且当顶层包重命名时,所有的文件都需要修改。
我搜了半天也没找到有文章提到abs比rel一定要好的理由,除了PEP8提到的两个缺乏说服力的词more portable and more readable。
4.explicit relative import有一个小问题:
Relative imports use a module's __name__ attribute to determine that module's position in the package hierarchy. If the module's name does not contain any package information (e.g. it is set to '__main__') then relative imports are resolved as if the module were a top level module, regardless of where the module is actually located on the file system.
 
 
Note that both explicit and implicit relative imports are based on the name of the current module. Since the name of the main module is always "__main__" , modules intended for use as the main module of a Python application should always use absolute imports.
 
PEP 338 -- Executing modules as scripts:  http://www.python.org/dev/peps/pep-0338/
 
虽然PEP338搞定了explicit relative import 在-m执行时的问题,但是搞不定直接文件执行的情况。所以用来做程序入口的模块必须使用绝对引用。
看一下这个例子:
$ python --version
Python 2.6.5
 
$ cat bar/baz.py 
from . import xml
print xml.__file__
 
$ python -m bar.baz
bar/xml.pyc
 
$ python bar/baz.py 
Traceback (most recent call last):
  File "bar/baz.py", line 1, in <module>
    from . import xml
ValueError: Attempted relative import in non-package

ValueError: Attempted relative import in non-package的更多相关文章

  1. ValueError: attempted relative import beyond top-level package

    python 项目 在pycharm中, 在某个文件夹下: 右键--> mark directory as --> source root 如何在python脚本或者shell中 用代码实 ...

  2. Python 相对导入attempted relative import beyond top-level package

    ValueError: attempted relative import beyond top-level package 假设有如下层次包目录 project/ __init__.py mypac ...

  3. Python ValueError: Attempted relative import in non-package Relative import相对引用 错误

    包含相对路径import的python脚本不能直接运行,只能作为module被引用. 例如 from . import mod1 有这样代码的文件只能最为moulule为不能直接运行.相对路径就是相对 ...

  4. Python导入自定义类时显示错误:attempted relative import beyond top-level package

    显示这个错误可能有两个原因: 1.文件夹中没有包含__init__.py文件,该文件可以为空,但必须存在该文件. 2.把该文件当成主函数入口,该文件所在文件夹不能被解释器视作package,所以可能导 ...

  5. python引入pytesseract报错:ValueError: Attempted relative import in non-package

    http://blog.csdn.net/yifengfuxue/article/details/79015651

  6. python:Attempted relative import in non-package

    problem:Attempted relative import in non-package 所谓相对路径其实就是相对于当前module的路径,但如果直接执行脚本,这个module的name就是“ ...

  7. 执行代码出现ImportError:attempted relative import with no known parent package

    前言 在这篇文章中,我将会解析 ImportError: attempted relative import with no known parent package 这个异常的原因.当你在运行的py ...

  8. Python工程:ImportError: attempted relative import with no known parent package

    Python工程:ImportError: attempted relative import with no known parent package 解决方法: 1.对每个目录创建的时候都选择创建 ...

  9. python相对包导入报“Attempted relative import in non-package”错误

    文章是从stackoverflow翻译过来的,原文地址:Relative imports for the billionth time 本文要在原理上解决  python当中相对包导入出现的问题. 问 ...

随机推荐

  1. 企业SAAS的春天,将以手机应用的形式,即将到来

    派尔科技吴春福 *本文是派尔为什么要投身企业移动应用的内部分享文章: *我没有仔细核查资料,仅代表个人看法,思路也是在整理过程,逻辑未必很完整,看官将就着看. 企业SAAS,概念起源是N年前,先行者也 ...

  2. PHP学习笔记 - 入门篇(4)

    PHP学习笔记 - 入门篇(4) 什么是运算符 PHP运算符一般分为算术运算符.赋值运算符.比较运算符.三元运算符.逻辑运算符.字符串连接运算符.错误控制运算符. PHP中的算术运算符 算术运算符主要 ...

  3. Java+Mysql+学生管理系统

    最近正在学java和数据库,想起以前写的学生管理系统,都是从网上下载,敷衍了事.闲来无事,也就自己写了一个,不过功能实现的不是很多. 开发语言:java: 开发环境:Mysql, java: 开发工具 ...

  4. 暑假集训(4)第六弹——— 组合(poj1067)

    题意概括:上一次,你成功甩掉了fff机械兵.不过,你们也浪费了相当多的时间.fff团已经将你们团团包围,并且逐步 逼近你们的所在地.面对如此危机,你不由得悲观地想:难道这acm之路就要从此中断?虽然走 ...

  5. C++四种强制类型转换关键字

    C语言使用强制类型转换(Type Cast)很简单,不管什么类型的转换,形式都如下: TYPE b = (TYPE)a; c++提供了4种类型转换操作符来应对不同场合的应用. const_cast   ...

  6. L007-oldboy-mysql-dba-lesson07

    L007-oldboy-mysql-dba-lesson07 [root@web01 ~]# mysqldump -uroot -ptestpassword -A >/root/mysql_ba ...

  7. poj 1659 Frogs' Neighborhood Havel-Hakimi定理 可简单图定理

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098136.html 给定一个非负整数序列$D=\{d_1,d_2,...d_n\}$,若存 ...

  8. web浏览器下的缓存 - Etag

    设置浏览器缓存的几种方法: Last-Modified : 服务器上文件的最后修改时间 Etag  : 文件标识 Expiers : 本地缓存目录中文件过期的时间 ( 由服务器指定具体的时间 ) Ca ...

  9. Android桌面悬浮清内存app概述

    今天闲来无事写了一个清内存的小东西,类似360,在桌面上悬浮,点击后清除后台无用程序,清除后台程序是通过调用ActivityManger.killBackgroundProcesses的方式来进行的, ...

  10. vhost设定

    vhost设定   http.conf <Directory /> AllowOverride none #Require all denied </Directory>   ...