【问题标题】:Module import path模块导入路径
【发布时间】:2009-11-22 04:45:40
【问题描述】:

我无法测试运行我想使用的 cssparser。

test.py

from css.parse import parse

data = """
    em {
    padding: 2px; 
    margin: 1em;
    border-width: medium;
    border-style: dashed;
    line-height: 2.4em;
    }
    p { color: red; font-size: 12pt }
    p:first-letter { color: green; font-size: 200% }
    p:first-line { color: blue }"""


for rule in parse(data):
    print (rule)

..给出一个错误

Traceback (most recent call last):
  method <module> in test.py at line 1
    from css.parse import parse
  method <module> in test.py at line 6
    from . import css, csslex, cssyacc
  method <module> in test.py at line 8
    from . import serialize
  method <module> in test.py at line 6
    from . import css
ImportError: cannot import name css

目录结构(/Users/nimbuz/Documents/python31):

/Users/nimbuz/Documents/python31/**csspy**/
|
+-- css/ (*has __init__.py*)
|
+-- uri/ (*has __init__.py*)
|
+-- test.py

print(sys.path) 显示:

['/Users/nimbuz/Documents/python31/csspy', '/Library/Frameworks/Python.framework/Versions/3.1/lib/python31.zip', '/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1', '/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/site-packages']

【问题讨论】:

标签: python import path module


【解决方案1】:

这是我刚刚测试的几个步骤。

  • readme说是python 2.5,所以需要python 2.x系列

  • 我创建了一个文件夹C:/TEST/

  • 我已将所有文件从css-py svn 下载到C:/TEST/,所以C:/TEST/css/C:/TEST/uri/ 文件夹现在存在。

  • 我已经下载了ply's tar gz file,并ply文件夹解压到C:/TEST/css/,所以C:/TEST/css/ply/文件夹现在存在。

  • 我在C:/TEST/ 中创建了test.py 的内容

    from css.parse import parse
    print dir(parse)
    
  • 我运行它,结果是这样的,没有导入错误:

    C:\TEST&gt;test.py

    ['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']

希望这会有所帮助。对不起,如果我的解释不好。

【讨论】:

【解决方案2】:

cssparsercss 目录中是否有 __init__.py 文件,以便将目录转换为包?是否有一个 css/parse.py 文件,其中包含一些名为 parse 的函数或类?当您执行test.py 时,您的sys.path 上有什么,当前目录是什么?所有这些问题都可能很重要,而您却一个都没有给我们答案,所以很难得到任何具体的帮助!-)

【讨论】:

  • sys.path: /Users/nimbuz/Documents/Python3/cssparser
  • 是的,它确实有那个文件:all = ('csslex', 'cssyacc', 'css', 'serialize', 'parse') – Nimbuz 1 分钟前
  • 您到底遇到了什么导入错误?如果您的 sys.path 只是 /Users/nimbuz/Documents/Python3/cssparser,则有问题。那里应该有一个目录列表。
  • 我不认为您尝试使用的 css 包可以支持 3.1(在您运行 2to3 后需要手动编辑,需要在两个版本的 Python 中都有丰富经验的人理想情况下也在包装本身上)。
  • 因此,同意 Alex 的观点,这个包及其依赖项并不是开始探索 Python 3 的好选择。
【解决方案3】:

python 3 版本的 css-py

http://ifile.it/v32n70s/css.zip

【讨论】:

  • 谢谢,但它仍然有很多错误。看起来它毕竟不适用于3.1。不过还是谢谢。
【解决方案4】:

您的css/ 是否有__init__.py

【讨论】:

    【解决方案5】:

    不要调用您自己的文件css.py。将其命名为其他名称,例如 css_test.py。同时删除css.pyc 文件。

    【讨论】:

      猜你喜欢
      • 2010-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多