views:

134

answers:

5
+1  Q: 

Module import path

I'm unable to test-run a cssparser that I'd like to use.

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)

..gives an error:

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

Directory structure (/Users/nimbuz/Documents/python31):

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

print(sys.path) shows:

['/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']
+2  A: 

Do you have __init__.py files in the cssparser and css directories, to turn the directories into packages? Is there a css/parse.py file with some function or class named parse in it? What's on your sys.path, and what's the current directory, when you execute test.py? All of these questions may be important, and you give us the answer to none of them, so it's hard to be of any specific help!-)

Alex Martelli
sys.path: /Users/nimbuz/Documents/Python3/cssparser
Nimbuz
Yes, it does have that file: all = ('csslex', 'cssyacc', 'css', 'serialize', 'parse') – Nimbuz 1 min ago
Nimbuz
Exactly what Import error are you getting? Something's wrong if your `sys.path` is just `/Users/nimbuz/Documents/Python3/cssparser`. There should be a list of directories there.
Ned Deily
SYSPATH: ['/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']
Nimbuz
ERROR: Traceback (most recent call last): File "/Users/nimbuz/Documents/python31/csspy/test.py", line 1, in <module> from css.parse import parse File "/Users/nimbuz/Documents/python31/csspy/css/parse.py", line 6, in <module> from . import css, csslex, cssyacc File "/Users/nimbuz/Documents/python31/csspy/css/css.py", line 8, in <module> from . import serialize File "/Users/nimbuz/Documents/python31/csspy/css/serialize.py", line 6, in <module> from . import cssImportError: cannot import name css
Nimbuz
@Nimbuz, you never ask my first and most crucial question, so I repeat it: "Do you have __init__.py files in the cssparser and css directories?". I see that shortly after my answer sykora asked exactly the same question in _his_ answer. BTW, your latest listing of a weirdly capitalized `SYSPATH` is really very, **totally** different from what you claimed to have on `sys.path` in your first comment! Why not edit your question with precise, non-contradictory and unambiguous answers to all of these questions?
Alex Martelli
Sorry about that, I've edited the question now with all the details.
Nimbuz
I don't think the css package you're trying to use can support 3.1 (it will need manual edits after your run of `2to3` on it, requiring somebody highly experienced in both versions of Python and ideally on the package itself too).
Alex Martelli
I can reproduce exactly the problem after running css through 2to3. There appears to be a circular import problem which can be avoided by changing or even removing the `from . import css` in serialize.py. I must admit it is escaping me at the moment what the problem there is. However, things don't get much further after that because you start to run into conversion problems in ply/yacc.
Ned Deily
So, to concur with Alex, this package and its dependencies aren't a good choice to begin exploring Python 3.
Ned Deily
Right, it seemed a small package that I could test-run, but it isn't. Thanks anyway Alex and Ned! :)
Nimbuz
A: 

Does your css/ have an __init__.py?

sykora
+4  A: 

Here is few steps I just tested.

  • readme says its python 2.5, so you need python 2.x series

  • I have created a folder C:/TEST/

  • I have downloaded all files from css-py svn to C:/TEST/, so C:/TEST/css/ and C:/TEST/uri/ folders exists now.

  • I have downloaded ply's tar gz file and extract only ply folder into C:/TEST/css/, so C:/TEST/css/ply/ folder exists now.

  • I have created test.py in C:/TEST/ with the content

    from css.parse import parse
    print dir(parse)
    
  • and I run it and the results is like this, without import errors:

    C:\TEST>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']

Hope this helps. sorry If my explanation is bad.

S.Mark
Many thanks for the step-by-step guide. But I did an additional step: converted all the files for 3.1 using 2to3 converter. My directory structure is similar but still no luck.
Nimbuz
Ah, that explains a lot. See section A.10 at Dive Into Python 3: http://diveintopython3.org/porting-code-to-python-3-with-2to3.html and PEO 328: http://www.python.org/dev/peps/pep-0328/
Greg Hewgill
er, that's PEP not PEO above.
Greg Hewgill
+2  A: 

python 3 version of css-py

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

jack
Thanks, but it still has tons of errors. Looks like it won't work on 3.1 after all. Thanks anyway though.
Nimbuz
A: 

Don't call your own file css.py. Name it something else, like css_test.py. Delete the css.pyc file also.

nosklo
Tried it, still the same.
Nimbuz